再次与 ember-data 作斗争 >.< ,这里的错误是在 FixtureAdapter 上,每当我使用 hasmany 关系时,我都会收到错误 加载路线时出错:TypeError {}
无法调用未定义的方法“toString””
库版本
Ember: 1.1.3+pre.e0ffbf84
Ember Data: 1.0.0-beta.3
Handlebars: 1.0.0
jQuery: 1.9.1
代码,也是here in a JSBin
取消注释项目:Facture 模型中的 hasMany 行以重现错误..
感谢您的帮助,这可能是一个错误,在这种情况下我将在 github 上发布问题。
// Setup
App = Ember.Application.create({});
App.ApplicationAdapter = DS.FixtureAdapter;
// Models
// Facture
App.Facture = DS.Model.extend({
title: DS.attr(),
createdAt: DS.attr('date', { defaultValue: new Date() })
// Comment this line out and it does display a title
//,items: DS.hasMany('item', { embedded: true })
});
// - Items
App.Item = DS.Model.extend({
desc: DS.attr(),
qty: DS.attr("number", { defaultValue: 0 }),
price: DS.attr("string", { defaultValue: 0 })
});
// FIXTURES
App.Facture.FIXTURES = [
{
id: 1,
title: "Services Informatiques",
createdAt: new Date(),
items: [
{ id: 1, desc:'Keay', qty: 2, price: "45" },
{ id: 2, desc:'You kidding', qty: 5, price: "75" }
]
},
{
id: 2,
title: "Intégration Web",
createdAt: new Date(),
items: [
{ id: 1, desc:'lkelzekekl', qty: 2, price: "250" },
{ id: 2, desc:'You', qty: 5, price: "200" }
]
}
];
// Routes
App.IndexRoute = Ember.Route.extend({
model: function(){
return this.store.find('facture');
}
});
最佳答案
默认情况下,RESTAdapter
和 FixtureAdapter
似乎不支持嵌入式关联。只需ActiveModelAdapter
。
有一些解决方法,通过 FixtureAdapter
,您可以使用您的灯具,而无需嵌入数据:
App.Facture.FIXTURES = [
{
id: 1,
title: "Services Informatiques",
createdAt: new Date(),
items: [1,2]
},
{
id: 2,
title: "Intégration Web",
createdAt: new Date(),
items: [3,4]
}
];
App.Item.FIXTURES = [
{ id: 1, desc:'Keay', qty: 2, price: "45" },
{ id: 2, desc:'You kidding', qty: 5, price: "75" },
{ id: 3, desc:'lkelzekekl', qty: 2, price: "250" },
{ id: 4, desc:'You', qty: 5, price: "200" }
]
这是一个包含此示例的 jsbin http://jsbin.com/oTIkigAV/9/edit
要与ActiveModelAdapter
一起使用,只需提供配置:
App.ApplicationAdapter = DS.ActiveModelAdapter;
// serializer for Facture model
App.FactureSerializer = DS.ActiveModelSerializer.extend({
attrs: {
items: {embedded: 'always'}
}
});
在 hasMany('item') 中使用 {embedded: true} 对我不起作用:(。
并且您的有效负载需要具有 type:embeddedModel
属性。在您的情况下输入:'item'
。就像下面这样:
{
factures: [
{
id: 1,
title: "Services Informatiques",
createdAt: newDate(),
items: [
{
id: 1,
desc: 'Keay',
qty: 2,
price: "45",
type: 'item'
},
{
id: 2,
desc: 'Youkidding',
qty: 5,
price: "75",
type: 'item'
}
]
},
{
id: 2,
title: "Intégration Web",
createdAt: newDate(),
items: [
{
id: 3,
desc: 'lkelzekekl',
qty: 2,
price: "250",
type: 'item'
},
{
id: 4,
desc: 'You',
qty: 5,
price: "200",
type: 'item'
}
]
}
]
};
还有一个带有事件模型适配器的 jsbin http://jsbin.com/oTIkigAV/10/edit
关于javascript - Ember 数据 FixtureAdapter 有很多 - 无法调用未定义的方法 'toString',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20124728/