Skip to content

Instantly share code, notes, and snippets.

@jugglinmike
Created May 30, 2014 19:11
Show Gist options
  • Save jugglinmike/a647bbd02251cc2a24af to your computer and use it in GitHub Desktop.
Save jugglinmike/a647bbd02251cc2a24af to your computer and use it in GitHub Desktop.
Experimenting with "firstChange" event in Ember
var onNextChange = function(model, handler, context) {
var fields = Ember.get(model.constructor, 'fields').keys.toArray();
// Avoid multiple invocations from batched changes using a boolean guard.
var hasChanged = false;
var handleChange = function() {
if (hasChanged) {
return;
}
hasChanged = true;
fields.forEach(function(field) {
model.removeObserver(field, null, handleChange);
});
handler.call(context || this);
};
fields.forEach(function(field) {
model.addObserver(field, null, handleChange);
});
};
// usage:
var BMac = DS.Model.extend({
setupWasModified: function() {
this.set('wasModified', false);
onNextChange(this, function() {
this.set('wasModified', true);
}, this);
}.on('init')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment