Working with Ember observers in Ember.js

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.

How to do it...

  1. To begin, we'll add a new observer called isOnChanged. This will only trigger when the isOn 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() {

Get Ember.js Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.