如何在Ember模型中实现排序,而不使用Array Controller? [英] How is Sorting achieved in an Ember model without using Array Controller?

查看:86
本文介绍了如何在Ember模型中实现排序,而不使用Array Controller?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个google结果都是一个ArrayController排序。需要排序机制而不使用ArrayController。



有一个模型,其中有排序参数。像sortOrder一样,作为模型中的一个属性(将来自后端)。



将使用#each渲染此模型,但这应该是基于sortOrder属性的迭代,而不是模型的ID属性。

解决方案

在Ember 2.0中 SortableMixin 已被弃用,并正在出路。



控制器(不是 ArrayController )您可以定义新的计算属性,如 SortedUsers1,2,3 以下:

 导出默认Ember.Controller.extend({
sortProps:['lastName'],
sortedUsers1:Ember.computed.sort 'model','sortProps'),
sortedUsers2:Ember.computed.sort('content','sortProps'),
sortedUsers3:Ember.computed('content',function(){
return this.get('content')。sortBy('lastName');
})
});

上面的假设是模型本身是一个用户数组, lastName 作为用户属性之一。依赖于'model''content'看起来相当于我。以上所有三个计算属性都产生相同的排序列表。



请注意,您不能用'sortProps' code>'lastName'在 sortedUsers1,2 - 它将无法正常工作。



要更改排序顺序修改 sortProps

  sortProps: ['lastName:desc'] 

如果您的模板位于users / index文件夹中,那么您的控制器必须也在那里用户/控制器不会,即使路由加载模式在用户/.



在模板中,使用情况如预期的那样:

 < ul> 
{{#each sortedUsers1 as | user |}}
< li> {{user.lastName}}< / li>
{{/ each}}
< / ul>


Every google result is about an ArrayController sorting. Need a sorting mechanism without using ArrayController.

There is a model where there are sort params. Like say 'sortOrder' as one of the properties in the model (which will be from a back end).

Will be rendering this model using #each but this should do the iteration based on the sortOrder property and not the model's ID property.

解决方案

In Ember 2.0 SortableMixin is deprecated and is on its way out too.

In the Controller (not the ArrayController) you may define a new computed property like SortedUsers1,2,3 below:

export default Ember.Controller.extend({
    sortProps: ['lastName'],
    sortedUsers1: Ember.computed.sort('model', 'sortProps'),
    sortedUsers2: Ember.computed.sort('content', 'sortProps'),
    sortedUsers3: Ember.computed('content', function(){
        return this.get('content').sortBy('lastName');
    })
});

The assumption above is that the model itself is an array of users with lastName as one of user properties. Dependency on 'model' and 'content' look equivalent to me. All three computed properties above produce the same sorted list.

Note that you cannot replace 'sortProps' argument with 'lastName' in sortedUsers1,2 - it won't work.

To change sorting order modify sortProps to

sortProps: ['lastName:desc']

Also if your template is in users/index folder then your controller must be there as well. The controller in users/ would not do, even if the route loading model is in users/.

In the template the usage is as expected:

    <ul>
        {{#each sortedUsers1 as |user|}}
            <li>{{user.lastName}}</li>
        {{/each}}
    </ul>

这篇关于如何在Ember模型中实现排序,而不使用Array Controller?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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