- Ember.js Cookbook
- Erik Hanchett
- 639字
- 2021-07-16 12:58:01
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:
- 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 observe changes and dynamically update whenever they occur.
- To see this in action, we can change the preceding example and set the
isOn
property tofalse
. Use the following code to accomplish this:bulb.set('isOn', true); bulb.get('description') //The yellow light is set to true
The description has been automatically updated and will now display
The yellow light is set to true
.
Chaining the Light object
Ember provides you with a nice feature that allows computed properties to be present in other computed properties. In the previous example, we created a description
property that outputted some basic information about the Light
object.
- Let's add another property that gives a full description:
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') }), });
- The
fullDescription
function returns a string that concatenates the output from the description with a new string that displaysage
:const bulb = Light.create({age:22}); bulb.get('fullDescription'); //The yellow light is set to false and the age is 22
In this example, during the instantiation of the
Light
object, we set theage
to22
. We could have overwritten any property if necessary.
Alias
The Ember.computed.alias
method allows us to create a property that is an alias for another property or object.
- Any call to
get
orset
will behave as if the changes were made to the original property: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') }), aliasDescription: Ember.computed.alias('fullDescription') }); const bulb = Light.create({age: 22}); bulb.get('aliasDescription');//The yellow light is set to false and the age is 22.
- The
aliasDescription
alias will display the same text asfullDescription
as it's just an alias of this object. If we made any changes to any properties in theLight
object later, the alias would also observe these changes and be computed properly. We'll discuss more about this in the Working with bindings recipe.
How it works...
Computed properties are built on top of the observer pattern. Whenever an observation shows a state change, it recomputes the output. If no changes occur, then the result is cached.
In other words, computed properties are functions that get updated whenever any of their dependent values change. You can use them in much the same way that you would use a static property. They are common and useful throughout Ember and its codebase.
Keep in mind that a computed property will only update if it is in a template or function that is being used. If the function or template is not being called, nothing will occur. This will help with performance.