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

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

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

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

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:


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

    Item: {{item.label}}


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);

    controller: App.ItemsController.create({
        content: [{ label: 'My first item' },
                  { label: 'My second item' },
                  { label: 'My third item' }]
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

使用声明 | 英豪名录