Search code examples
javascriptember.jsember-dataember-router

Emberjs: How do I filter users by any property using routes


I have the following route and model setup. All my users display fine using a template and displays the firstName and email as expected. However I am unable to filter by firstName or email when routing similar to how :user_id works. (I am using the fixture adapter for now)

Example: #/users/logan

 this.resource('users', function(){
     this.resource('user', {path: ':user_firstName'}); //#/users/logan
 });

The following model

App.User = DS.Model.extend({
    firstName: DS.attr('string'),
    email: DS.attr('string')
})

So to summarise further, exactly what should happen inside the model function or what other approach can I take?

App.UsersRoute = Em.Route.extend({
    model: function(){
        var user = App.User.find();;

       //FILTER BY firstName how?

        return user;
    }
});

Solution

  • You can create an event on the route that changes the controller content to be a filter of the model.

    Template:

    <span {{action filterByName}}>Filter by name</span>

    Router:

    App.UsersRoute = Ember.Route.extend({
      events: {
        filterByName: function() {
          this.get('controller').set('content', this.get('currentModel').filterProperty('firstName', 'John'));
        }
      }
    });