3

问题摘要:虽然我可以让集合的子代(在 ArrayController 上定义)为个人使用特定的对象控制器,但这不适用于过滤的子集子集。

简短的上下文:我有订阅,其中有项目。我想按类型过滤视图中的订阅,并让这些订阅中的项目按时间戳排序。这是订阅控制器:

Social.SubscriptionsController = Ember.ArrayController.extend({
  itemController: 'subscription',

  announcements: function() {
     return this.get('model').filterBy('kind', 'announcement');
  }.property('model.@each.kind'),

  user_sites: function() {
    return this.get('model').filterBy('kind', 'user');
  }.property('model.@each.kind')
});

我已经这样定义了 SubscriptionController:

Social.SubscriptionController = Ember.ObjectController.extend({
  items: function() {
    return Ember.ArrayProxy.createWithMixins(Ember.SortableMixin, {
      sortProperties: ['post_timestamp'],
      sortAscending: false,
      content: this.get('content.items')
    });
  }.property('content.items'),
});

这是我的车把模板的相关部分:

{{#each controller}}
  <li>{{controller.description}} {{controller.kind}} {{controller.feed_url}} {{controller.base_url}}</li>
  <ul>
    {{#each item in controller.items}}
      <li>{{item.post_timestamp}}: {{{item.summary}}}</li>
    {{/each}}
  </ul>
{{/each}}

该代码或多或少地做了我想要的:它呈现按 item.post_timestamp 排序的项目,正如 SubscriptionController 定义的那样。

问题是如果我更改{{#each controller}}{{#each site in user_sites}}, itemController 属性似乎不会神奇地应用于子列表。是否有某种巫术我应该在我的过滤器中通知 Ember 我宁愿返回对象的控制器而不是对象本身?

编辑添加:我知道我可以在 Subscription 模型本身上添加一个新属性,如 sorted_items ,但这感觉不对,设计方面。模型保存数据,视图显示数据,控制器处理排序/过滤和所有这些爵士乐。或者至少这是我对 MVC 分离的看法的一部分。

4

1 回答 1

4

您可以手动设置itemControllerfor 循环。你可以在你的模板中试试这个:

{{#each site in user_sites itemController="subscription"}}
...
{{/each}}
于 2013-09-06T19:54:13.410 回答