我的Handlebar文件看起来有点像这样:
{{#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“。有什么想法吗?
发布于 2013-02-16 16:54:18
嗯,一开始我认为根据this post和this 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
https://stackoverflow.com/questions/14907164
复制相似问题