emberjs绑定数据属性

时间:2012-10-09 18:13:37

标签: ember.js

我想知道在调用视图时是否有办法在模板中绑定数据属性。

例如(这不起作用):

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

我最终这样做了:

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

调用视图时必须有办法吗?

3 个答案:

答案 0 :(得分:13)

更多关于@ kurt-ruppel的优秀答案。

使用:描述属性绑定属性的示例,完全从视图中完成。

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

清洁模板。

{{view App.SomeView}}

答案 1 :(得分:10)

您必须在App.SomeView中定义要放入HTML的属性。

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

现在data-dateBinding应该有效:

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

答案 2 :(得分:2)

FWIW - 这是对@ sly7_7顶部答案的评论的回应 - 可以在视图中指定data- *属性绑定,而不是在模板中设置它。

classNameBindings类似,您可以在属性前添加首选值,并使用“:”连接值。看到这个行动的最佳地点可能是the related ember.js tests。让人相信良好测试的价值,看看它有时候是最好的文档。

相关问题