Sathya
Sathya

Reputation: 5308

EmberJS - How to filter array and pass it to template via router

see this jsFiddle.

I have array like this.

App.Fields = [{id: "Gender", desc: "Male", key: "M"}, 
                  {id: "Gender", desc: "Female", key: "F"}, 
                  {id: "Martial Status", desc: "Single", key: "S"}, 
                  {id: "Martial Status", desc: "Married", key: "M"}];   

While my router executes i want to pass filtered array based model.id. Below is my router.

App.FieldRoute = Ember.Route.extend({
  setupController: function(controller, Field) {
    controller.set('model', App.Fields);
  }
});

When user clicks "Gender" i want to filter objects having id = "Gender" from the above array.

FYI: model.id will give "Gender".

Upvotes: 1

Views: 559

Answers (2)

selvagsz
selvagsz

Reputation: 3872

You could also use .filterProperty() which returns the array with the matched property

controller.set('model', App.Fields.filterProperty('id',model.id));

will also do the trick

Upvotes: 0

Sathya
Sathya

Reputation: 5308

This works.

App.FieldRoute = Ember.Route.extend({    
  setupController: function(controller, model) {   
    controller.set('model', App.Fields.filter(function(item, index, enumerable) {
        if(item.id === model.id) return true;
    }));
  }
});

Upvotes: 1

Related Questions