Inserting external parameter in map function with ember

46 views Asked by At

I have this function:

this.get('model').map(function(item){
            if(item.get('isSelected')){
                item.set('selectedClass', 'conversation-history');
            }
        }); 

What I would like to do :

this.get('model').map(function(item){
            if(item.get('id') == this.get('selectedConvId')){
                item.set('selectedClass', 'conversation-history');
            }
        }); 

This tells me this.get is not a function, and I understand why, I am not in the same scope. But how can I introduce an external parameter in the map function? Thank you

2

There are 2 answers

0
artych On BEST ANSWER

Use variable var self = this; or var selectedConvId = this.get('selectedConvId');

for example

var self = this;
this.get('model').map(function(item){
  if(item.get('id') == self.get('selectedConvId')){
     item.set('selectedClass', 'conversation-history');
  }
});
0
Nick V On

Also, I advise you to use Ember.set or Ember.get. It works with Ember objects and plain javascript objects as well.