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.
Let's create a new Ember.Object and add a computed property to it:
Let's begin by creating a new
description
computed property. This property will reflect the status of theisOn
andcolor
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'); }) });
We can now create a new
Light
object and get the computed propertydescription
:const bulb = Light.create(); bulb.get('description'); //The yellow light is set to false
The preceding example creates a computed property that depends on the
isOn
andcolor
properties. When thedescription
function is called, it returns a string describing the state of the light.Computed properties will...