ember.js - Ember Computed Property vs Ember Observer -


none of previous questions saw on here seemed cover topic of when use ember computed property vs ember observer. understand computed property uses previous attributes generate new attribute , updated in run loop.

person = ember.object.extend({   // these supplied `create`   firstname: null,   lastname: null,    fullname: ember.computed('firstname', 'lastname', function() {     return `${this.get('firstname')} ${this.get('lastname')}`;   }) }); 

an observer on other hand updated outside of run loop, , can watch computed property. reacts type of change.

person = ember.object.extend({   // these supplied `create`   firstname: null,   lastname: null,    fullname: ember.computed('firstname', 'lastname', function() {     return `${this.get('firstname')} ${this.get('lastname')}`;   }),    fullnamechanged: ember.observer('fullname', function() {     // deal change     console.log(`fullname changed to: ${this.get('fullname')}`);   }) }); 

the ember documentation states observers typically on used. can give better example of correct usage of observers? else can watch, , effects of incorrect usage vs correct usage?

source code can found on ember documentation: https://guides.emberjs.com/v2.3.0/object-model/observers/

computed property uses previous attributes generate new attribute , updated in run loop

yes, lazily--meaning when referenced, and dependencies changing has invalidated cached value.

the ember documentation states observers typically on used.

yes, observers fire synchronously whenever watched property changes, if reason recompute won't used. using observers should computed properties 1 of classic ember anti-patterns.

i checked couple of large applications worked on , found observers being used things calling third-party library necessary when changed, or changing app's language when new ui language chosen.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -