Charles
Charles

Reputation: 11768

EmberJS - Observe a computed property

Why the following codes are not equivalent?

With computed:

computed: Ember.computed('selected', function() {
    console.log('computed');
    return this.get('selected');
}),

observer1: Ember.observer('computed', function() {
    console.log('observer1');
}),

observer2: Ember.observer('selected', function() {
    console.log('observer2');
}),

With alias:

computed: Ember.computed.alias('selected'),

observer1: Ember.observer('computed', function() {
    console.log('observer1');
}),

observer2: Ember.observer('selected', function() {
    console.log('observer2');
}),

The first one only prints observer2 and the second one prints observer1 and observer2.

Upvotes: 2

Views: 256

Answers (1)

Ebrahim Pasbani
Ebrahim Pasbani

Reputation: 9406

In the first one, computed is just a getter. So if you don't use computed in template or other places to react ASAP on changing, it's not called by changing selected.

But in second one, alias creates setter too. So if you change selected, computed changes after that quickly.

If you use computed in first one in a template, the same result comes.

Upvotes: 1

Related Questions