gpt4 book ai didi

javascript - Ember 数据 createRecord 不工作 : "TypeError: undefined is not a function"

转载 作者:行者123 更新时间:2023-11-29 19:36:25 25 4
gpt4 key购买 nike

我正在为 Web 应用程序使用 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

有人知道这是为什么吗?这让我发疯!

最佳答案

从 Ember Data 的 1.0beta+ 开始,不需要定义存储。您只需定义适配器和序列化程序。

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();

示例(为了好玩而延迟):http://emberjs.jsbin.com/OxIDiVU/856/edit

关于javascript - Ember 数据 createRecord 不工作 : "TypeError: undefined is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24941332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com