【问题标题】:How can I use a custom/dynamic attribute for a view in Ember JS?如何在 Ember JS 中为视图使用自定义/动态属性?
【发布时间】:2013-02-16 05:06:57
【问题描述】:

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

【问题讨论】:

    标签: ember.js handlebars.js


    【解决方案1】:

    嗯,起初我以为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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-21
      • 2017-08-20
      相关资源
      最近更新 更多