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

Nud*_*oll 4 handlebars.js ember.js

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

{{#each book in view.bookcase}}
  {{view App.BookView classBinding="book.read:read:unread"}}
{{/each}}
Run Code Online (Sandbox Code Playgroud)

我想添加一个属性,book-id="1"或者当前书的ID是什么,但我不知道如何.如果我试试这个......

{{#each book in view.bookcase}}
  {{view App.BookView book-id="book.id" classBinding="book.read:read:unread"}}
{{/each}}
Run Code Online (Sandbox Code Playgroud)

...然后该属性字面上设置为"book.id".有任何想法吗?

Lio*_*han 5

嗯,起初我认为attributeBindings根据这篇文章这篇文章允许使用,但是当我尝试这样做时,我得到这个错误:

Uncaught Error: assertion failed: Setting 'attributeBindings' via Handlebars is not allowed. Please subclass Ember.View and set it there instead.
Run Code Online (Sandbox Code Playgroud)

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

车把模板:

<script type="text/x-handlebars">
    {{#each book in view.bookcase}}
        {{view App.BookView classBinding="book.read:read:unread" contentBinding="book"}}
    {{/each}}
</script>
Run Code Online (Sandbox Code Playgroud)

扩展课程:

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

示例:jsFiddle代码段