Sort computed property on ArrayController

I have the following jsbin: http://jsbin.com/okoxim/4/edit

filterContent is a computed property that filters the contents of my controller. I want to know how to sort a computed property and any ways to improve the code that I have.

App.StudentsController = Ember.ArrayController.extend({ sortProperties: ['name'], nameFilter: null, filteredContent: function(){ if(!this.get('nameFilter')) return this.get('content'); var nameRegEx = new RegExp(this.get('nameFilter'), 'i'); return this.filter(function(item) { return item.get('name').search(nameRegEx) !== -1; }); }.property('nameFilter', '@each.name') }); 
+6
source share
1 answer

The easiest way is to wrap the result in ArrayProxy , which is sorted by the same sortProperties values. Sort of,

 filteredContent: function(){ var result; if (!this.get('nameFilter')) { result = this.get('content'); } else { var nameRegEx = new RegExp(this.get('nameFilter'), 'i'); result = this.filter(function(item) { return item.get('name').search(nameRegEx) !== -1; }); } var sortedResult = Em.ArrayProxy.createWithMixins( Ember.SortableMixin, { content:result, sortProperties: this.sortProperties } ); return sortedResult; }.property('nameFilter', '@each.name') 

Here is updated jsbin .

Another way is to create a filteredContent explicit ArrayProxy and filter / sort.

+8
source

Source: https://habr.com/ru/post/949699/


All Articles