Kumar
Kumar

Reputation: 51

What is the difference between this.set and Ember.set

Is there any difference between following two conditions this.set('firstName','ember') and Ember.set(this,'firstName','ember')

Upvotes: 5

Views: 5274

Answers (1)

bsegraves
bsegraves

Reputation: 1020

Ember's computed property chain and observers rely on you calling object.set() on the object in question. You will often see warnings to this effect if you forget.

However, it's possible to get into a situation where you may not know if the object you've been handed is an Ember.Object or just a plain javascript object. That's where Ember.set() comes in handy - it works on both types of objects.

const setFoo(target, value) {
    Ember.set(target, 'foo', value);
}

const a = {foo:null};
const b = Ember.Object.create({foo:null});

// both calls are valid because we use Ember.set()
setFoo(a, 'johnny');
setFoo(b, 'bravo');

Here's the official documation that's kinda helpful: Ember.set()

Upvotes: 8

Related Questions