【发布时间】:2014-02-01 07:56:46
【问题描述】:
让我用帖子和评论来描述我的问题。在我的 post_controller 中,我希望为当前帖子的评论创建一条新记录。这样做的余烬方法是什么?
关系是这样设置的:
App.Post = DS.Model.extend({
comments: hasMany('comment'),
});
App.Comment = DS.Model.extend({
post: belongsTo('post')
});
在我的 post_controller 中,我想创建一条记录。我在一个从模板触发的动作中包含了这个:
App.PostController = Ember.ObjectController.extend({
...
actions: {
createComment: function() {
var post = this.get('model'); // Edit: Forgot that I had this declared outside createRecord
var comment = this.store.createRecord('comment', {
content : "content",
post : post // This is where the problem is
});
}
}
});
但是,我收到一条错误消息:Uncaught TypeError: Cannot read property 'post' of undefined
我如何声明这种关系?谢谢。
编辑: ember-data 错误来自 ember-data.js 中的这个内部函数:
return Ember.computed(function(key, value) {
var data = get(this, 'data'),
store = get(this, 'store'), belongsTo, typeClass;
if (typeof type === 'string') {
typeClass = store.modelFor(type);
} else {
typeClass = type;
}
if (arguments.length === 2) {
Ember.assert("You can only add a '" + type + "' record to this relationship", !value || value instanceof typeClass);
return value === undefined ? null : value;
}
belongsTo = data[key]; // ERROR OCCURS HERE!
if (isNone(belongsTo)) { return null; }
store.fetchRecord(belongsTo);
return belongsTo;
}).property('data').meta(meta);
};
编辑:问题解决了!
问题是我给了注释一个叫做数据的属性。该属性与内部余烬冲突。删除它使我上面的代码可以正常工作。
【问题讨论】:
标签: javascript ember.js ember-data