Observers are fundamental to the Ember object model. In the next recipe, we'll take our light example, add an observer, and see how it operates.
To begin, we'll add a new observer called
isOnChanged
. This will only trigger when theisOn
property changes:const Light = Ember.Object.extend({ isOn: false, color: 'yellow', age: null, description: Ember.computed('isOn','color',function() { return 'The ' + this.get('color') + 'light is set to ' + this.get('isOn') }), fullDescription: Ember.computed ('description','age',function() { return this.get('description') + ' and the age is ' + this.get('age') }), desc: Ember.computed.alias('description'), isOnChanged: Ember.observer('isOn',function() { console.log('isOn value changed') }) }); const bulb = Light.create({age: 22}); bulb.set('isOn',true); //console logs isOn value changed
Ember.observer
isOnChanged
monitors theisOn
property. If any changes occur to this...