Created
August 1, 2017 18:32
-
-
Save typeoneerror/961cc3dbd01c037f7e61620101049c95 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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