简体   繁体   English

在Ember中按不同模型的属性排序

[英]Sort with different models' properties in Ember

I understand that you can sort different properties in ascending/descending orders by doing the following: 我了解您可以通过执行以下操作以升序/降序对不同的属性进行排序:

App.TestsController = Ember.ArrayController.extend(
    sortProperties: ['testDate:desc', 'name:asc']
    sortedObjects: Ember.computed.sort('content', 'sortProperties')
)

and then referencing sortedObjects in {{#each}}. 然后在{{#each}}中引用sortedObjects。 However, in the case that each test has a user associated with it, is there a way we can also sort by the user's properties? 但是,在每个测试都有一个与之关联的用户的情况下,我们是否可以通过该方法对用户的属性进行排序? I've noticed that when I just sort by test properties or user properties, everything works fine, but when I do this: 我注意到,当我仅按测试属性或用户属性排序时,一切正常,但是当我这样做时:

App.TestsController = Ember.ArrayController.extend(
    sortProperties: ['testDate:desc', 'user.lastName:asc']
    sortedObjects: Ember.computed.sort('content', sortProperties')
)

I get an "Index out of range" error. 我收到“索引超出范围”错误。 Will I need to use my own sort to accommodate sorting through different models? 我是否需要使用自己的排序来适应不同模型的排序?

Try binding the object property to the controller: 尝试将对象属性绑定到控制器:

App.TestsController = Ember.ArrayController.extend(
    needs: ['user'],
    lastName: '',
    lastNameBinding: 'controllers.user.lastName',
    sortProperties: ['testDate:desc', 'lastName:asc']
    sortedObjects: Ember.computed.sort('content', sortProperties')
)

You could also try a computed property. 您也可以尝试计算属性。

So what ended up being the issue was how I was setting up connections between tests and users. 所以最终成为问题的是我如何在测试和用户之间建立连接。 At first I was connecting users to tests in the setupController, which resulted in users being returned as promises to the controller. 最初,我将用户连接到setupController中的测试,这导致将用户作为对控制器的承诺返回。 When I moved the process to the model hook, my loading screen was no longer showing up. 当我将流程移至模型挂钩时,不再显示加载屏幕。 If anyone runs into the same problem, I followed this article by Balint Erdi. 如果有人遇到相同的问题,我将在本文后面关注Balint Erdi。

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

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