Working with computed properties

In this recipe, we'll take a look at computed properties and how they can be used to display data, even if that data changes as the application is running.

How to do it...

Let's create a new Ember.Object and add a computed property to it:

  1. Let's begin by creating a new description computed property. This property will reflect the status of the isOn and color properties:
    const Light = Ember.Object.extend({
      isOn: false,
      color: 'yellow',
    
      description: Ember.computed('isOn','color',function() {
        return 'The ' + this.get('color') + ' light is set to ' + this.get('isOn');
      })
    
    });
  2. We can now create a new Light object and get the computed property description:
    const bulb = Light.create(); bulb.get('description'); //The yellow light ...

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.