在ArrayController模型的已过滤子集上设置itemController [英] Setting itemController on a filtered subset of an ArrayController's model

查看:147
本文介绍了在ArrayController模型的已过滤子集上设置itemController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Problem Summary: While I can get the children of a collection (defined on an ArrayController) to use a specific object controller for the individuals, this doesn't work on filtered subsets of the children.

短上下文:我有Subcriptions,它们有Items。我想按照类型筛选我的视图中的订阅项,并将这些订阅中的项目按时间戳排序。这是订阅控件:

Short context: I've got Subcriptions, which have Items. I'd like to filter the subscriptions in my view by type, and have the items within those subscriptions sort by timestamp. Here's the SubscriptionsController:

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:

I've defined SubscriptionController thusly:

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'),
});

这里是我的手柄模板的相关位:

And here's the relevant bit of my handlebars template:

{{#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}}

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

That code more-or-less does what I want: it renders the items, sorted by item.post_timestamp, as SubscriptionController defines it.

问题是如果我更改 {{#每个控制器} } {{#user_sites中的每个网站}} ,itemController属性似乎并不适用于子列表。有没有什么样的巫术我应该用来通知我的过滤器,我宁愿返回控制器的对象而不是对象本身?

The problem is if I change {{#each controller}} to {{#each site in user_sites}}, the itemController property doesn't seem to magically apply to the sublist. Is there some kind of Sorcery I should use to inform Ember in my filters that I'd rather return the controller for the objects rather than the objects themselves?

编辑添加:我知道我可以在Subscription模型本身添加一个像sorted_items这样的新的属性,但是设计上是错误的。该模型保存数据,视图显示数据,控制器处理排序/过滤以及所有爵士乐。或者至少这是我如何考虑MVC分离的一部分。

EDITed to add: I know I can just add a new property like sorted_items on the Subscription model itself, but this feels wrong, design-wise. The model holds the data, the view shows the data, and the controller deals with sorting / filtering and all that jazz. Or at least that's part of how I think about MVC separation.

推荐答案

您可以手动设置 itemController for循环。您可以在模板中尝试:

You can manually set the itemController for loops. You might try this in your template:

{{#each site in user_sites itemController="subscription"}}
...
{{/each}}

这篇关于在ArrayController模型的已过滤子集上设置itemController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆