emberjs bind data attributes

倖福魔咒の 提交于 2019-12-23 06:58:59

问题


I am wondering if there is a way to bind data attributes in a template when calling a view.

For example (this doesn't work):

 {{ view App.SomeView data-dateBinding="currentDate" }}

I have ended up doing it this way:

<a {{bindAttr data-date="currentDate"}}></a>

There must be a way to do it when calling the view?


回答1:


You have to define in App.SomeView which attributes you want put in the HTML.

App.SomeView = Ember.View.extend({
  attributeBindings: ["data-date"]
  .... rest of view
})

Now data-dateBinding should work:

{{view App.SomeView data-dateBinding="currentDate" }}



回答2:


More on the excellent answer from @kurt-ruppel.

An example using : to describe the property for attribute binding, entirely done from the view.

App.SomeView = Ember.View.extend({
  attributeBindings: ["date:data-date"],
  date: function() { return '1642-12-06' }
  .... rest of view
})

The cleaner template.

{{view App.SomeView}}



回答3:


FWIW - and this is in response to @sly7_7's comments from the top answer -, it is possible to specify a data-* attribute binding in the view itself, as opposed to setting it in the template.

Similar to classNameBindings, you can prepend a preferred value to the attribute, joining the values with a ':'. Best place to see this in action is probably in the related ember.js tests. Gives credence to the value of good testing, seeing as how sometimes it serves as the best documentation.



来源:https://stackoverflow.com/questions/12805942/emberjs-bind-data-attributes

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