无法在Ember中的View的ObjectController上将attributeBindings设置为属性

时间:2013-04-25 09:15:25

标签: javascript ember.js

我设法通过我的把手模板将attributeBinding设置为控制器上下文:

<li {{bindAttr class="view.controller.isSelected:active:"}}>
    <a href="#" {{action "toggleSelected"}}>{{view.controller.title}}</a>
</li>

在这样的每个组中调用:

<ul class="nav nav-tabs nav-stacked">
    {{#each skill in controller}}
        {{view App.SkillView}}
    {{/each}}
</ul>

但我发现EmberJS在每个视图周围添加了div,导致我的CSS出现问题所以我从模板中删除了li标签,并在SkillView中设置了tagName,并尝试通过JS实现了attributeBinding在我想要的地方添加li DOM元素的对象,但我使用的attributeBindings似乎不起作用:

App.SkillView = Ember.View.extend({
    templateName: 'skill',
    tagName: 'li',
    attributeBindings: ['controller.isSelected:active:']
});

这也不是:

App.SkillView = Ember.View.extend({
    templateName: 'skill',
    tagName: 'li',
    attributeBindings: ['view.controller.isSelected:active:']
});

也不是这样:

App.SkillView = Ember.View.extend({
    templateName: 'skill',
    tagName: 'li',
    attributeBindings: ['isSelected:active:']
});

如何在视图绑定字符串的上下文中从视图中引用控制器?我在Handlebars模板上做了什么,我没有在View代码中做什么?

1 个答案:

答案 0 :(得分:1)

原来我应该使用classNameBindings而不是attributeBindings。以下工作:

App.SkillView = Ember.View.extend({
    templateName: 'skill',
    tagName: 'li',
    classNameBindings: ['controller.isSelected:active:']
});