正在获取ember.js模型的可枚举属性childs



我正试图从视图didInsertElement钩子获取我的模型数据,因为模板渲染完成后,我需要在DOM上触发一些操作

具有以下型号:

App.MyParent = DS.Model.extend({
    title: DS.attr('string'),
    childs: DS.hasMany('App.MyChild')
});
App.MyChild = DS.Model.extend({
    name: DS.attr('string'),
    parent: DS.belongsTo('App.MyParent')
});

以及像这样的对象

App.MyParent.FIXTURES = [
    { id: 0, title: "some title", childs: [0,2,3] }
]
App.MyChild.FIXTURES = [
    { id: 0, name: "some name", parent: 0 },
    { id: 2, name: "other name", parent: 0 },
    { id: 3, name: "diff name", parent: 0 }
]

在钩子函数中,我将访问childs属性,如下所示:

var childs = this.get("context.content.childs");

然后我得到了一个具有正确数量的子对象的可枚举对象,但子对象都有未定义的属性。

更新似乎我唯一可以用App.MyChild.find(someId)找到孩子的地方是在浏览器控制台中,在我的应用程序代码中,我只得到未定义属性的对象。

我很困惑!

要访问childs属性,请尝试以下操作:

var childs = this.get("context.content.childs");

此外,关联的fixture数据不应使用_id或_ids。因此,请使用childsparent

App.MyParent.FIXTURES = [
  { id: 0, title: "some title", childs: [0,2,3] },
];
App.MyChild.FIXTURES = [
  { id: 0, name: "some name", parent: 0 },
  { id: 2, name: "other name", parent: 0 },
  { id: 3, name: "diff name", parent: 0 },
];

http://jsbin.com/ucanam/316/

最新更新