Emberjs: how to filter more than one property at once

Below I filter one property confidently, but how can I filter another at a time? That is, without providing the user with a drop-down list containing various search parameters Example. My search term can be name, email address or age.

var search = this.controllerFor('employees').search; //can be name, email or age employees = this.get('currentModel').filterProperty('name', search); 

The above works great for updating the main list, but I can only filter one property at a time.

 //Sample Model App.Employee = DS.Model.extend({ email: DS.attr('string'), name: DS.attr('string'), age: DS.attr('number'), }) 

One thought is to filter the filter again if the filter result is length = 0 , and the other how to combine the results. However, I am not big on this idea and believe that Amber may be the best - a more elegant way to achieve this.

+8
javascript ember-data
source share
1 answer

You can use the filter function to filter several properties in your model and even use other controller properties. For example:

Imagine a model like this:

 App.Person = DS.Model.extend({ firstName: DS.attr('string'), lastName: DS.attr('string'), fullName: function() { return '%@ %@'.fmt( this.get('firstName'), this.get('lastName') ); }.property('firstName', 'lastName') }); 

to filter by several properties, let's say you have a controller with a search function like this:

 ... performSearch: function(searchTerm) { return this.get('content').filter(function(person) { return person.get('firstName').indexOf(searchTerm) !== -1 || person.get('lastName').indexOf(searchTerm) !== -1; }); }, ... 

This will repeat the contact list in content and apply one or more filters that return only model objects that match the filter.

Fiddle: http://jsfiddle.net/schawaska/ABJN7/

+8
source share

All Articles