Ember ArrayController无法按itemController中定义的属性排序 [英] Ember ArrayController cannot be sorted by property defined in itemController

查看:79
本文介绍了Ember ArrayController无法按itemController中定义的属性排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按在itemController中定义/计算的属性对ArrayController进行排序。参见此 JSBin 。如果您按firstName(在模型中定义)排序,则可以正常工作,但如果您按lastName(在itemController中定义)进行排序,则无法正常工作。确保使用sortAscending:true或false。知道如何进行这项工作吗?

I want to sort an ArrayController by a property defined/computed in the itemController. See this JSBin. If you sort by firstName (defined in the model), it works fine, but if you sort by lastName (defined in the itemController), it doesn't work. Make sure to play with sortAscending: true or false. Any idea how to make this work?

这是另一个更简单的 JSBin 表现出相同的行为(第一个JSBin更接近我的实际代码)。

Here is another simpler JSBin that exhibits the same behavior (the first JSBin is closer to my actual code).

推荐答案

可排序的混合应用于内容,而不应用于内容的控制器。

The sortable mixin is applied on the content, not on the controllers of the content.

代码: https://github.com/emberjs/ember.js/ blob / v1.1.2 / packages / ember-runtime / lib / mixins / sortable.js#L72

您可能想添加任何逻辑

您之前提到的特定用例最适合该模型。确实,您在控制器和模型上画线的地方是一团糟。如果属性需要在控制器之间持久存在,则应将其添加到模型中,尤其是在控制器不是单例控制器的情况下。如果它是单例控制器,并且模型在其下方永远不变,则该属性可以存在于控制器上。

The particular use case that you mentioned before was best suited on the model. Really the place where you draw the line on controller and model is wishy washy. If the property needs to persist across controllers then you should add it to the model, especially if the controller isn't a singleton controller. If it's a singleton controller and the model never changes underneath it, then the property can live on the controller.

重要的是要注意,在模型上定义属性并不意味着您不必从服务器获取属性,也不必将其保存到服务器。

It's important to note that defining a property on the model doesn't mean you have to get it from the server, nor save it to the server.

App.User = DS.Model.extend({
  name : DS.attr(),  // this will be saved to the server
  something: 31      // this isn't a DS attr, it isn't going anywhere
});

作为注释,我早先撒谎了一些东西。

As a note, I lied earlier about something.

您可以从父控制器与子控制器进行对话。

You can talk to your child controllers from your parent controller.

从父控制器内部,您可以使用objectAt访问子控制器并在

From inside of the parent controller, you can access the child controllers using objectAt and iterating over the parent controller.

在此示例中,这是父控制器

  console.log(this.objectAt(0));

  this.forEach(function(itemController){
    console.log(itemController);
  });

http://emberjs.jsbin.com/AQijaGI/1/edit

这篇关于Ember ArrayController无法按itemController中定义的属性排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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