Search code examples
ember.jsember-cli

Ember How to pass a variable to Ember.computed.filterBy


Imagine I have a component with 2 properties: How would I be able to create a computed property based on the localID property and the Ember.computed.filterBy macro ?

localID: 2,

data: [
{
    id:1,
    values: [1,2,3]
},
{
    id:2,
    values: [4,5,6]
},
{
    id:3,
    values: [7,8,9]
}],

I have tried:

filteredData: Ember.computed.filterBy('data', 'id', 'localID') // localID gets treated as a string

filteredData: Ember.computed.filterBy('data', 'id', localID) // localID not defined

filteredData: Ember.computed.filterBy('data', 'id', this.get('localID')) // 'this' not valid in this context..

filteredData: Ember.computed.filterBy('data', 'id', ${localID}) // etc..

Nothing seems to work.. Of course if I statically input my value there it works but I wanted it to reference the other property in this case localID as it will be passed from a controller.

Thanks for any help..


Solution

  • Rewrite your property as

    filteredData: function() {
      return this.get('data').filterBy('id', this.get('localID'));
    }.property('data.@each.id')