Is it logical to use ObjectController and ArrayController together?

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
depends 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.

The question is: where should I compute this isSelected
property ? 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.

You’ve got an example here: http://jsfiddle.net/MikeAski/r6xcA/

Handlebars:

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



    Item: {{item.label}}

​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();
​
Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 前端开发 » Is it logical to use ObjectController and ArrayController together?

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录