4

我的 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”。有任何想法吗?

4

1 回答 1

5

嗯,起初我认为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 片段

于 2013-02-16T08:54:18.043 回答