Bạn phải sắp xếp theo cách thủ công các mảng, bao gồm bất kỳ nội dung của bộ điều khiển mảng nào, trong Ember. Bạn luôn có thể thay thế content
bằng mảng đã sắp xếp của mình thay vì giữ cả hai mảng.
Cập nhật
Dưới đây là một ví dụ về những gì tôi nghĩ rằng bạn đang tìm kiếm: http://jsfiddle.net/ud3323/yjs8D/
Update # 2
Cập nhật ví dụ này để sử dụng những thay đổi cái nhìn bối cảnh mới. https://gist.github.com/2494968
tay lái
<script type="text/x-handlebars" >
Will Display and Sort by Age (after 2 sec)<br/><br/>
{{#each App.userController}}
{{#view App.RecordView}}
{{name}} - {{age}}
{{/view}}
{{/each}}
</script>
Javascript
App = Ember.Application.create({
ready: function() {
Ember.run.later(this, function() {
Ember.run.later(this, function() {
App.get('userController').set('content', [
Ember.Object.create({ name:"Jeff", age:24 }),
Ember.Object.create({ name:"Sue", age:32 }),
Ember.Object.create({ name:"Jim", age:12 })
]);
}, 2000);
Ember.run.later(this, function() {
// Make the controller's content sort again in reverse this time
App.userController.notifyPropertyChange('content');
}, 4000);
}
});
App.userController = Ember.ArrayController.create({
content: [],
contentDidChange: Ember.observer(function(userCtr, prop) {
if(!Ember.empty(this.get('content'))) {
console.log('about to begin sort');
this.sortContent();
}
this._super();
}, 'content'),
sort:"desc",
sortContent:function() {
var content = this.get("content"), sortedContent;
if (this.get("sort") == "desc") {
this.set("sort", "asc");
sortedContent = content.sort(function(a,b){
return a.get("age") - b.get("age");
});
} else {
this.set("sort","desc");
sortedContent = content.sort(function(a,b){
return b.get("age") - a.get("age");
});
}
this.set("content",sortedContent);
}
});
App.RecordView = Ember.View.extend({});
Nguồn
2012-05-08 14:42:39
thể trùng lặp của [Sắp xếp mảng các đối tượng EmberJS bởi tài sản] (http://stackoverflow.com/questions/9041505/sort-array-of-emberjs-objects-by-property) –