Created
May 30, 2014 19:11
-
-
Save jugglinmike/a647bbd02251cc2a24af to your computer and use it in GitHub Desktop.
Experimenting with "firstChange" event in Ember
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
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