- 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.
- Boost.Asio C++ Network Programming(Second Edition)
- Spring Cloud Alibaba微服務架構設計與開發實戰
- Oracle Exadata性能優化
- Spring Boot+Spring Cloud+Vue+Element項目實戰:手把手教你開發權限管理系統
- Big Data Analytics
- Java編程技術與項目實戰(第2版)
- JavaCAPS基礎、應用與案例
- PHP+MySQL+Dreamweaver動態網站開發從入門到精通(第3版)
- Mastering Data Mining with Python:Find patterns hidden in your data
- Responsive Web Design with jQuery
- 高性能MVVM框架的設計與實現:San
- 微服務設計
- VMware vRealize Orchestrator Essentials
- JavaScript前端開發程序設計教程(微課版)
- 微信小程序開發圖解案例教程:附精講視頻(第3版)