对ArrayController的内容进行排序

时间:2012-05-08 12:50:19

标签: arrays sorting ember.js

我的Ember.ArrayController内容未分类。

我想知道是否可以在不使用新属性的情况下对ArrayController的内容进行排序。

我当然可以创建一个新属性:

App.MyArrayController = Em.ArrayController.extend({
  mySortMethod: function(obj1, obj2) {
    // some code here
  },
  updateSortedContent: function() {
    var content = this.get('content');
    if (content) {
      var sortedContent = content.copy();
      sortedContent.sort(this.mySortMethod);
      this.set('sortedContent', sortedContent);
    }
  }.observes('content')
});

但我希望有更好的方法不会重复内容。

2 个答案:

答案 0 :(得分:35)

<强>更新

Ember的最新版本实际上已经内置了排序。ArrayController现在包含Ember.SortableMixin,如果您指定sortProperties(数组)和可选sortAscending(布尔值),它将会参与)。

注意:使用新的SortableMixin,您仍然需要引用arrangedContent来获取排序版本。模型本身将保持不变。 (感谢Jonathan Tran)

App.userController = Ember.ArrayController.create({
  content: [],
  sortProperties: ['age'],
  sortAscending: false
})

原始回答

执行此操作的正确方法是使用ArrayProxy的arrangedContent属性。此属性旨在被覆盖以提供内容数组的已排序或过滤版本。

App.userController = Ember.ArrayController.create({
  content: [],
  sort: "desc",
  arrangedContent: Ember.computed("content", function() {
    var content, sortedContent;
    content = this.get("content");
    if (content) {
      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");
        });
      }
      return sortedContent;
    } else {
      return null;
    }
  }).cacheable()
});

答案 1 :(得分:2)

您必须在Ember中手动对数组进行排序,包括任何数组控制器的内容。您始终可以将content替换为已排序的数组,而不是保留两个数组。

<强>更新

以下是我认为您正在寻找的示例:http://jsfiddle.net/ud3323/yjs8D/

更新#2

更新了此示例以使用新的视图上下文更改。 https://gist.github.com/2494968

<强>车把

<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({});

相关问题