在使用Ember数据创建记录时,如何创建嵌套/嵌入式模型?具体来说,我想创建一个带有嵌套/嵌入式模型作者的post模型。下面的代码给出了错误:
处理路由时出错:索引断言失败:不能将“未定义”记录添加到“post.Author”。您只能在这种关系中添加“作者”记录。错误:断言失败:不能将“未定义”记录添加到“post.Author”中。您只能在这种关系中添加“作者”记录。
App.IndexRoute = Ember.Route.extend({
model: function() {
return this.store.createRecord('post', {
title: 'My first post',
body: 'lorem ipsum ...',
author: {
fullname: 'John Doe',
dob: '12/25/1999'
}
});
}
});
App.Post = DS.Model.extend({
title: DS.attr('string'),
body: DS.attr('string'),
author: DS.belongsTo('author')
});
App.Author = DS.Model.extend({
fullname: DS.attr('string'),
dob: DS.attr('string')
});对怎么做有什么想法吗?我还在JSBin:http://emberjs.jsbin.com/depiyugixo/edit?html,js,console,output上创建了一个演示
谢谢!
发布于 2015-07-21 18:50:48
关系需要分配给实例化的模型,普通对象不能工作。
App.IndexRoute = Ember.Route.extend({
model: function() {
return this.store.createRecord('post', {
title: 'My first post',
body: 'lorem ipsum ...',
author: this.store.createRecord('author', {
fullname: 'John Doe',
dob: '12/25/1999'
})
});
}https://stackoverflow.com/questions/31546681
复制相似问题