Вопрос по arrays, sorting – Сортировать содержимое ArrayController

8

Error: User Rate Limit ExceededEmber.ArrayControllerError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

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')
});

Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededSort array of EmberJS objects by property Willem de Wit

Ваш Ответ

2   ответа
35

UPDATE

Error: User Rate Limit ExceededArrayControllerError: User Rate Limit ExceededEmber.SortableMixinError: User Rate Limit ExceededsortPropertiesError: User Rate Limit ExceededsortAscendingError: User Rate Limit Exceeded

Error: User Rate Limit ExceededarrangedContentError: User Rate Limit Exceeded

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

ORIGINAL ANSWER

Error: User Rate Limit ExceededarrangedContentError: User Rate Limit Exceeded

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()
});
Error: User Rate Limit ExceededcontentError: User Rate Limit Exceeded
Error: User Rate Limit ExceededarrangedContentError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededgist.github.com/3744939
2

content

Update

Update #2

Handlebars

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

Error: User Rate Limit Exceeded louiscoquio
Error: User Rate Limit Exceededthis._super()Error: User Rate Limit Exceeded louiscoquio
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded_super()Error: User Rate Limit ExceededcontentDidChangeError: User Rate Limit ExceededEmber.ArrayProxyError: User Rate Limit ExceededcontentError: User Rate Limit ExceededEmber.ArrayProxyError: User Rate Limit ExceededcontentError: User Rate Limit Exceeded

Похожие вопросы