ember.js #each order by property

我的梦境 提交于 2019-12-20 02:56:32

问题


I have an array of Ember.Objects which is displayed by Handlebars {{#each}} helper which I want to be sorted by an property of those objects everytime the array changes.

So something like this:

var arr = [
    Ember.Objects.create({
        position:0,
        label:"foo"
    }),
    Ember.Objects.create({
        position:1,
        label:"bar"
    }),
];

And the handlebar

{{#each arr}}
    <div class="label">{{label}}</div>
{{/each}}

So if I update the positions and the bar object becomes first, I want the view to be updated. Can I depend the {{#each}} Helper on an property?


回答1:


You have to use an ArrayController proxy on your data, and set the sortProperties attribute. Then, use the controller as the each data source.

Sample @ http://jsfiddle.net/MikeAski/Epjqp/

Using the controller as the data source provides an arranged content. Take care not to use directly the controller's content, as it is the raw source data...



来源:https://stackoverflow.com/questions/11205013/ember-js-each-order-by-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!