Skip to content

Instantly share code, notes, and snippets.

@typeoneerror
Created August 1, 2017 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save typeoneerror/961cc3dbd01c037f7e61620101049c95 to your computer and use it in GitHub Desktop.
Save typeoneerror/961cc3dbd01c037f7e61620101049c95 to your computer and use it in GitHub Desktop.
// mixins/dictionary-observable.js
import Ember from 'ember';
const {
Mixin,
on,
run
} = Ember;
const { once } = run;
export default Mixin.create({
dictionaryObservers: {},
isRollingBack: false,
addObservers() {
const observers = this.get('dictionaryObservers');
_.forEach(observers, (props, observer) => {
_.forEach(props, (prop) => {
const key = `${observer}.${prop}`;
this.addObserver(key, this, 'dictionaryDidChange');
});
});
},
initObservers: on('didLoad', function() {
this.addObservers();
}),
dictionaryDidChange(sender, key, value, rev) {
if (!this.get('isRollingBack')) {
once(this, 'dictionaryDirtiedModel');
}
},
dictionaryDirtiedModel() {
this.send('becomeDirty');
},
rollbackAttributes() {
this.set('isRollingBack', true);
this._super();
this.set('isRollingBack', false);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment