我使用Ember数据作为网络应用程序,一切都运行得非常顺利,直到我尝试使用" createRecord"保存一些新数据。
这是代码的要点:
App.Store = DS.Store.extend({
adapter: DS.RESTAdapter.create({
namespace: 'api'
})
});
App.Training = DS.Model.extend({
dateTimestamp: DS.attr('number'),
dateTime: DS.attr('string'),
dateDay: DS.attr('number'),
dateWeekday: DS.attr('string'),
dateMonth: DS.attr('string'),
teacherId: DS.attr('number'),
teacherFirstName: DS.attr('string'),
teacherPhoneNumber: DS.attr('string'),
teacherAvatarCss: DS.attr('string'),
spotName: DS.attr('string'),
spotNamespace: DS.attr('string'),
spotId: DS.attr('number'),
spotHasPicture: DS.attr('string'),
spotPictureCss: DS.attr('string'),
spotUrl: DS.attr('string')
});
我为后端构建了一个简单的REST api,并且获取数据效果很好:
App.KalenderRoute = Em.Route.extend({
model: function() {
return this.store.find('training'); // This works!
}
});
然后我尝试在我的Controller中调用一个动作来保存新记录:
App.KalenderController = Em.ArrayController.extend({
actions: {
addTraining: function(){
var store = this.store;
store.createRecord('training', {
spotId: 5
}); // This does not work :(
}
}
});
如果我已经正确理解,这应该使用spotId参数向/ api / training发送POST AJAX请求。但是,似乎从未发送过POST请求,而是我的控制台说:
TypeError: undefined is not a function
完整堆栈跟踪:
DS.Store.Ember.Object.extend.createRecord ember-data.prod.js:1575
App.KalenderController.Em.ArrayController.extend.actions.addTraining App.js:197
Mixin.create.send ember.prod.js:15207
runRegisteredAction ember.prod.js:34269
Backburner.run ember.prod.js:7947
apply ember.prod.js:7779
run ember.prod.js:6415
handleRegisteredAction ember.prod.js:34267
(anonymous function) ember.prod.js:22582
m.event.dispatch jquery.min.js:3
r.handle
有没有人知道为什么会这样?它让我发疯了!
答案 0 :(得分:3)
自从1.0beta +的Ember Data以来,不需要定义商店。您只需定义适配器和序列化器。
App.ApplicationAdapter= DS.RESTAdapter.extend({
namespace: 'api'
});
App.Training = DS.Model.extend({
dateTimestamp: DS.attr('number'),
dateTime: DS.attr('string'),
dateDay: DS.attr('number'),
dateWeekday: DS.attr('string'),
dateMonth: DS.attr('string'),
teacherId: DS.attr('number'),
teacherFirstName: DS.attr('string'),
teacherPhoneNumber: DS.attr('string'),
teacherAvatarCss: DS.attr('string'),
spotName: DS.attr('string'),
spotNamespace: DS.attr('string'),
spotId: DS.attr('number'),
spotHasPicture: DS.attr('string'),
spotPictureCss: DS.attr('string'),
spotUrl: DS.attr('string')
});
此外,在您在记录上调用保存
之前,不会进行任何ajax调用var record = this.store.createRecord('training',{
spotId:1
});
record.save();