繁体   English   中英

Ember.js:对ArrayController进行分组/分区

[英]Ember.js: Grouping/partitioning an ArrayController

我有一个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}}

我究竟做错了什么?

仅当您遍历控制器集合时, 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}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM