从ember.js视图访问DOM元素的属性 [英] Access attributes of the DOM elements from ember.js views

查看:87
本文介绍了从ember.js视图访问DOM元素的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从视图本身访问由ember.js视图插入的DOM元素的属性。这是一个简短的例子。可以说我有以下 cat.handlebars 模板:

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

在此视图中使用:



pre $ App.CatView = Ember.View.extend({
templateName:'cat',
catsBinding:'App。 catsController',
didInsertElement:() - >
#需要获取每个正在插入的DIV的ID以便为其添加一些JavaScript
console.log'context it',this 。$()
})

this。$() 返回一个非常深的嵌套对象,我无法在其中找到我的DIV的任何标志。当我位于 didInsertElement 函数内时, view.content.id 也没有被定义。

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

解决方案

您可以使用{{view.contentIndex}}而不是{{view.content.id}}。因为你已经为你的div添加了一个classname cat,所以你可以通过这个来访问它。$('#index.cat'),其中索引应该被你想访问的内容的索引所取代。


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.

解决方案

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.

这篇关于从ember.js视图访问DOM元素的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆