Last active
November 24, 2017 11:17
-
-
Save lifeart/f5397e1e38d2436bb2ddf7d80fd30cf3 to your computer and use it in GitHub Desktop.
Ember observers mixin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Mixin.create({ | |
lastChangedAttrs: null, | |
init() { | |
this.resetLastChangedAttrs(); | |
this.initObservers(); | |
this._super(...arguments); | |
}, | |
didChangeAttrs() { | |
console.log('not implemented'); | |
}, | |
createObserverFor(propertyName) { | |
return function (target, key) { | |
this.addChangedAttribute(propertyName); | |
}.bind(this); | |
}, | |
initObservers() { | |
(this.get('didChangeAttrsConfig.attrs') || []).forEach((attrName) => { | |
let observerName = `${attrName}@observer`; | |
this[observerName] = this.createObserverFor(attrName); | |
this.addObserver(attrName, this, observerName); | |
}); | |
}, | |
didUpdateAttrs() { | |
this.didChangeAttrs(this.lastChangedAttrs); | |
this.resetLastChangedAttrs(); | |
this._super(...arguments); | |
}, | |
addChangedAttribute(name) { | |
this.lastChangedAttrs[name] = this.get(name); | |
}, | |
resetLastChangedAttrs() { | |
this.lastChangedAttrs = {}; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment