【发布时间】:2013-02-20 11:11:21
【问题描述】:
createRecord 永远不会创建 belongsTo 对象。
在Post-> hasOne -> Comment 存在这种关系并且评论嵌入在帖子中总是的情况下,是否有任何解决方法可以创建子模型对象。
这适用于Post -> hasMany -> Comments(如在 ember-data-example 中。需要帮助,我们遇到了这个问题。
App.Test = DS.Model.extend({
text: DS.attr('string'),
contact: DS.belongsTo('App.Contact')
});
App.Contact = DS.Model.extend({
id: DS.attr('number'),
phoneNumbers: DS.hasMany('App.PhoneNumber'),
test: DS.belongsTo('App.Test')
});
App.PhoneNumber = DS.Model.extend({
number: DS.attr('string'),
contact: DS.belongsTo('App.Contact')
});
App.RESTSerializer = DS.RESTSerializer.extend({
init: function() {
this._super();
this.map('App.Contact', {
phoneNumbers: {embedded: 'always'},
test: {embedded: 'always'}
});
}
});
/* in some controller code */
this.transitionToRoute('contact', this.get('content'));
以下代码行:
this.get('content.phoneNumbers').createRecord();
以下代码行失败:
this.get('content.test').createRecord();
这是错误:
Uncaught TypeError: Object <App.Test:ember354:null> has no method 'createRecord'
因此 hasMany 与 createRecord 一起使用,但 1:1 失败。难道我做错了什么 ?什么必须是正确的方法/不可能做到这一点?
【问题讨论】:
-
@adrien coquio 谢谢,关于这个问题的任何线索?
-
我们没有足够的信息来正确查看您的问题。也许您可以尝试写一个jsfiddle 来重现问题?或者添加一些更明确的代码示例?
-
@adrien coquio 希望我的编辑对您有所帮助。寻求帮助,我们陷入了困境。
标签: ruby-on-rails mongodb ember.js ember-data