ontroller和ArrayController一起使用是否有

ontroller和ArrayController一起使用是否有

本文介绍了将ObjectController和ArrayController一起使用是否有意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个对象列表,存储在arrayController中,并使用 #each

I have a list of object, stored in an arrayController and rendered on the view using the #each macro

{{#each item in controller}}
  {{view App.ItemView}}
{{/each}}

每个项目视图具有取决于用户操作的类名称绑定。例如:

Each item view has class name binding that depends on the user action. For exemple :

App.ItemView = Ember.View.extend {
  classNameBindings: ['isSelected:selected']
}

isSelected 依赖于每个项目的状态:我必须将所选项目存储在某处,并将其与触发点击事件的新选定项目进行比较。

isSelecteddepends on the state of each Item : I have to store the selected item somewhere, and compare it to the new selected item if a click event is triggered.

问题是:where我应该计算这个 isSelected 属性吗?在 itemsController 中?在 itemController 中?直接在每个itemView中?

The question is: where should I compute this isSelectedproperty ? In the itemsController ? In an itemController? Directly in each itemView ?

推荐答案

对我来说,把它放入视图是有道理的,而且它真的是显示关注。

To me, it does make sense to put it into the view as, moreover, it is really a display concern.

这里有一个例子:

Handlebars

<script type="text/x-handlebars" data-template-name="items">
    {{#each item in controller}}
        {{view App.ItemView contentBinding="item"}}
    {{/each}}
</script>

<script type="text/x-handlebars" data-template-name="item">
    Item: {{item.label}}
</script>

JavaScript

App.ItemsController = Ember.ArrayController.extend({
    selected: null
});

App.ItemsView = Ember.View.extend({
    templateName: 'items'
});

App.ItemView = Ember.View.extend({
    templateName: 'item',
    classNameBindings: ['isSelected:selected'],

    isSelected: function() {
        var item = this.get('content'),
            selected = this.getPath('controller.selected');
        return item === selected;
    }.property('item', 'controller.selected'),

    click: function() {
        var controller = this.get('controller'),
            item = this.get('content');
        controller.set('selected', item);
    }
});

App.ItemsView.create({
    controller: App.ItemsController.create({
        content: [{ label: 'My first item' },
                  { label: 'My second item' },
                  { label: 'My third item' }]
    })
}).append();
​

这篇关于将ObjectController和ArrayController一起使用是否有意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 12:43