- Ember.js Cookbook
- Erik Hanchett
- 551字
- 2021-07-16 12:58:02
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...
- 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 property,isOnChanged
is invoked.Note
Computed properties versus observers
At first glance, it might seem that observers are the same as computed properties. In fact, they are very different. Computed properties can use
get
andset
methods and can be used in templates. Observers, on the other hand, just monitor property changes and cannot be used in templates or be accessed like properties. They don't return any values as well. With this said, be careful not to overuse observers. In many instances, a computed property is a more appropriate solution. - Additionally, if needed, you can add multiple properties to the observer. Just use the following code:
Light.reopen({ isAnythingChanged: Ember.observer('isOn','color',function() { console.log('isOn or color value changed') }) }); const bulb = Light.create({age: 22}); bulb.set('isOn',true); // console logs isOn or color value changed bulb.set('color','blue'); // console logs isOn or color value changed
The
isAnything
observer is invoked whenever theisOn
orcolor
properties change. The observer will fire twice as each property has changed.
Synchronous issues with the Light object and observers
It's very easy to get observers out of sync. If, for example, a property that it observes changes, it will be invoked as expected. After being invoked, it might manipulate a property that hasn't been updated yet. This can cause synchronization issues as everything happens at the same time.
- The following example shows this behavior:
Light.reopen({ checkIsOn: Ember.observer('isOn', function() { console.log(this.get('fullDescription')); }) }); const bulb = Light.create({age: 22}); bulb.set('isOn', true);
When
isOn
is changed it's not clear iffullDescription
, a computed property, has been updated yet or not. As observers work synchronously, it's difficult to tell what has been fired and changed. This can lead to unexpected behavior. - To counter this, it's best to use the
Ember.run.once
method. This method is a part of the Emberrun
loop, which is Ember's way of managing how code gets executed. Reopen theLight
object and you will see the following:Light.reopen({ checkIsOn: Ember.observer('isOn','color', function() { Ember.run.once(this,'checkChanged'); }), checkChanged: Ember.observer('description',function() { console.log(this.get('description')); }) }); const bulb = Light.create({age: 22}); bulb.set('isOn', true); bulb.set('color', 'blue');
The
checkIsOn
observer calls thecheckChanged
observer usingEmber.run.once
. This method gets run only once perrun
loop. Normally,checkChanged
would get fired twice; however, as it's being called usingEmber.run.once
, it outputs only once.
How it works...
Ember observers are mixins from the Ember.Observable
class. They work by monitoring property changes. When any change occurs, they are triggered. Keep in mind that these are not the same as computed properties and cannot be used in templates or with getters or setters.
- Java系統(tǒng)分析與架構(gòu)設(shè)計(jì)
- Django開發(fā)從入門到實(shí)踐
- Learn Programming in Python with Cody Jackson
- Instant QlikView 11 Application Development
- 你必須知道的204個(gè)Visual C++開發(fā)問題
- 單片機(jī)應(yīng)用技術(shù)
- 差分進(jìn)化算法及其高維多目標(biāo)優(yōu)化應(yīng)用
- 從Excel到Python:用Python輕松處理Excel數(shù)據(jù)(第2版)
- Apache Spark 2.x for Java Developers
- Lighttpd源碼分析
- RealSenseTM互動(dòng)開發(fā)實(shí)戰(zhàn)
- “笨辦法”學(xué)C語言
- 零基礎(chǔ)學(xué)C語言程序設(shè)計(jì)
- Oracle實(shí)用教程
- C# 7.1 and .NET Core 2.0:Modern Cross-Platform Development(Third Edition)