2012-09-19 44 views
4

我怎麼能訪問正在ember.js視圖插入,從視圖本身的DOM元素的屬性。這是一個簡短的例子。比方說,我有以下cat.handlebars模板:訪問屬性

{{#collection contentBinding="App.catsController"}} 
    <div class="cat" {{bindAttr id="view.content.id"}}></div> 
{{/collection}} 

這是在該視圖中使用:

App.CatView = Ember.View.extend({ 
    templateName: 'cat', 
    catsBinding: 'App.catsController', 
    didInsertElement:() -> 
    #need to get the id of each DIV that is being inserted to add some JavaScript for it 
    console.log 'context it ', this.$() 
}) 

this.$()返回一個嵌套很深的對象,我無法找到我在它的DIV的跡象。此外,當我是didInsertElement函數內部view.content.id沒有定義。

要重申我的問題,當我是一個觀點,我怎麼可以添加相關的一些正在通過視圖插入的DOM元素的一些JavaScript代碼裏面。

回答

2

您可以使用{{view.contentIndex}},而不是{{view.content.id}}。既然你已經添加了一個類名貓到你的div你可以使用此訪問它。$(「#index.cat」),其中指數應該要訪問內容的索引被替換。