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 的任何迹象。view.content.id当我在函数内部时也没有定义didInsertElement

重申我的问题,当我在视图中时,如何添加一些与视图插入的某些 DOM 元素相关的 Javascript 代码。

4

1 回答 1

2

您可以使用 {{view.contentIndex}} 代替 {{view.content.id}}。由于您已将类名 cat 添加到您的 div 中,您可以使用 this.$('#index.cat') 访问它,其中 index 应替换为您要访问的内容的索引。

于 2013-10-22T07:03:20.303 回答