我正在使用ember-data的fixtures适配器加载App.Structure对象.这些对象将App.Overlay对象嵌入为hasMany关系,如下所示:
App.Structure = DS.Model.extend({
name: DS.attr('string'),
// hash of overlay paths
overlays: DS.hasMany('App.Overlay', { embedded: true }),
});
App.Overlay = DS.Model.extend({
view: DS.attr('string'),
path: DS.attr('string')
});
App.Overlay.FIXTURES = [];
App.Structure.FIXTURES = [{
"id": 0,
"name": "Test Structure",
"overlays": [{
"view": "Isometric",
"path": "[elided to save space]"
}],
}];
(注意空夹具阵列是修复this issue.)
现在我可以加载App.Structure,但我无法从中获取任何东西.见this jsfiddle;可以访问结构,并且它能够遍历覆盖数组,但是数组中的对象是空心壳,其中没有任何内容.
这是一个控制台会话选择:
> currStructure = App.fixtureStore.findAll(App.Structure).get('firstObject');
Class
> currStructure.get('name');
"Test Structure"
> currStructure.get('overlays').get('length');
1
> var overlay = currStructure.get('overlays').get('firstObject');
undefined
> overlay instanceof App.Overlay;
true
> overlay.get('view');
null
> overlay.get('path');
null
为什么这不起作用?
ETA:在@Kristaps的回应之后,我更新了小提琴.现在第一个Overlay仍然只是一个空心壳,但任何后续叠加都不是.咦?
最佳答案 我想这是因为你没有在Overlay对象中的overlay对象和belongsTo关系中设置structure_id.看小提琴
http://jsfiddle.net/kristaps_petersons/REtNV/
App.Structure = DS.Model.extend({
id: DS.attr('number'),
name: DS.attr('string'),
overlays: DS.hasMany('App.Overlay', {embedded: true})
});
App.Overlay = DS.Model.extend({
id: DS.attr('number'),
vi: DS.attr('string'),
pa: DS.attr('string'),
structure: DS.belongsTo('App.Structure'),
});
和固定装置:
App.Overlay.FIXTURES = [];
App.Structure.FIXTURES = [
{
"id": 0,
"name": "Test Structure 0",
"overlays": [{
"id": 0,
"structure_id": 0,
"vi": "Overlay view 0.0",
"pa": "Overlay path 0.0"},
{
"id": 1,
"structure_id": 0,
"vi": "Overlay view 0.1",
"pa": "Overlay path 0.1"},
{
"id": 2,
"structure_id": 0,
"vi": "Overlay view 0.2",
"pa": "Overlay path 0.2"}
]},
{
"id": 1,
"name": "Test Structure 1",
"overlays": [{
"id": 3,
"structure_id": 1,
"vi": "Overlay view 1.0",
"pa": "Overlay path 1.0"},
{
"id": 4,
"structure_id": 0,
"vi": "Overlay view 1.1",
"pa": "Overlay path 1.1"},
{
"id": 5,
"structure_id": 0,
"vi": "Overlay view 1.2",
"pa": "Overlay path 1.2"}
]},
{
"id": 2,
"name": "Test Structure 2"}
];