【问题标题】:Ember.js: Grouping/partitioning an ArrayControllerEmber.js:对 ArrayController 进行分组/分区
【发布时间】:2014-05-15 17:38:57
【问题描述】:

我有一个 ArrayController,我想根据特定键的值对该 ArrayController 的内容进行分组。

例如,如果我的 ArrayController 中的对象是:

id   status   name
-----------------------------
1    1        some name
2    1        some other name
3    2        blah
4    3        blah again

那我想把内容按status分组。

为此,我尝试在我的 ArrayController 中使用计算属性:

App.SomeArrayController = Ember.ArrayController.extend({
  itemController: 'some-item',      

  status1: Ember.computed.filterBy('content', 'status', 1),
  status2: Ember.computed.filterBy('content', 'status', 2),
  status3: Ember.computed.filterBy('content', 'status', 3)
});

在模板中,这些正在显示,但它们没有被我在 ArrayController 中指定的 itemController 包装

// item controller used in the ArrayController
App.SomeItemController = Ember.ObjectController.extend({
  anotherKey: function () {
    return 'hey ' + this.get('name');
  }.property('name')
});


<!-- template to display status=1 items -->
{{#each status1}}

  // displays the name (as a property from the model)
  {{this.name}}

  // nothing displays here
  // (leading me to believe it is not being wrapped by the item controller)
  {{this.anotherKey}}
{{/each}}

我做错了什么?

【问题讨论】:

    标签: javascript ember.js


    【解决方案1】:

    itemController 仅在您遍历控制器集合时包装项目。

    {{#each item in controller}}
      {{item.coolItemControllerProperty}}
    {{/each}}
    

    它不适用于控制器中的任何集合。如果您尝试迭代底层模型/内容,它将不会被包装。

    {{#each item in content}}
      {{item.coolItemControllerProperty}} // undefined
    {{/each}}
    
    {{#each item in model}}
      {{item.coolItemControllerProperty}} // undefined
    {{/each}}
    

    幸运的是,您可以在模板中为这些情况指定 itemController

    {{#each item in status1 itemController='some-item'}}
      {{item.coolItemControllerProperty}}
    {{/each}}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-13
      • 1970-01-01
      • 1970-01-01
      • 2012-03-27
      • 2021-02-05
      • 2022-07-29
      • 1970-01-01
      相关资源
      最近更新 更多