Access attributes of the DOM elements from ember.js views

泪湿孤枕 提交于 2019-12-10 16:27:15

问题


How can I access attributes of DOM elements that are being inserted by ember.js view, from within the view itself. Here is a short example. Lets say I have the following cat.handlebars template:

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

which is used in this view:

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.$() returns a very deeply nested object and I can not find any sign of my DIVs in it. Also view.content.id is not defined when I am inside the didInsertElement function.

To reiterate my question, when I am inside a view, how can I add some Javascript code related to some of the DOM elements that are being inserted by the view.


回答1:


You can use {{view.contentIndex}} instead of {{view.content.id}}. Since you have added a classname cat to your div you can access it by using this.$('#index.cat'), where index should be replaced by the index of the content you want to access.



来源:https://stackoverflow.com/questions/12499921/access-attributes-of-the-dom-elements-from-ember-js-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!