2013-07-06 36 views
0

我試圖從視圖didInsertElement鉤到我的模型數據,因爲我需要觸發的DOM一些動作時,模板渲染完成前往ember.js模型枚舉財產童車

與像模型:

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 } 
] 

裏面的鉤子函數我得到的孩子的屬性是這樣的:

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

然後我得到了一個enumerable與正確數量的孩子,但th e孩子都有undefined屬性。

更新 好像在哪裏可以得到一個孩子App.MyChild.find(someId)的唯一地方是在瀏覽器控制檯,在我的應用程序代碼中,我只得到對象與不確定性質。

我很疑惑!

回答

1

要進入孩子的財產,嘗試這樣的事情:

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

另外,協會夾具數據不應該使用_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/

+0

從我個人理解,這是一樣的:var孩子的= this.get( 「context.content」)( '孩子的')。 ,無論如何,我得到相同的結果... – loopole

+0

也許你的意思是'this.get(「context.content」)。get('childs');'?在那種情況下,是一樣的。我認爲問題實際上是與燈具,更新我的答案... –

+0

我已經注意到,使用_childs_而不是_child_ids_,會產生一個可枚舉的結果,其中的元素:'child.get('name')'產生未定義。因此,如果使用_childs_而不是_child_ids_是正確的方法,那麼我的子對象仍然有問題? – loopole