如何在Ember JS中为视图使用自定义/动态属性?

时间:2013-02-16 05:06:58

标签: ember.js handlebars.js

我的Handlebars文件看起来有点像这样:

{{#each book in view.bookcase}}
  {{view App.BookView classBinding="book.read:read:unread"}}
{{/each}}

我想在book-id="1"的曲调或者当前图书的ID中添加一个属性,但我不知道如何。如果我试试这个......

{{#each book in view.bookcase}}
  {{view App.BookView book-id="book.id" classBinding="book.read:read:unread"}}
{{/each}}

...然后该属性字面上被设置为“book.id”。有什么想法吗?

1 个答案:

答案 0 :(得分:5)

嗯,起初我认为根据this postthis post允许使用attributeBindings,但是当我尝试这样做时,我会收到此错误:

Uncaught Error: assertion failed: Setting 'attributeBindings' via Handlebars is not allowed. Please subclass Ember.View and set it there instead.

所以我认为现在最好的方法是在课堂上进行。

车把模板:

<script type="text/x-handlebars">
    {{#each book in view.bookcase}}
        {{view App.BookView classBinding="book.read:read:unread" contentBinding="book"}}
    {{/each}}
</script>

扩展课程:

App.BookView = Ember.View.extend({
    attributeBindings: ['book-id'],
    'book-id': function() {
        return this.content.id;
    }.property('content.id')
});

示例:jsFiddle snippet