2013-02-16 20 views
4

我把手文件看起來有點像這樣:如何在Ember JS中爲視圖使用自定義/動態屬性?

{{#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」。有任何想法嗎?

回答

5

嗯,起初我以爲使用attributeBindings根據this postthis post允許的,但是當我嘗試這樣做,我得到這個錯誤:

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

相關問題