Skip to content

Instantly share code, notes, and snippets.

@pivotal-medici
Created July 23, 2012 19:59
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 pivotal-medici/3165855 to your computer and use it in GitHub Desktop.
Save pivotal-medici/3165855 to your computer and use it in GitHub Desktop.
handle multiple event handlers competing to modify and save an ember data object
/*
This code handles the case where your focusOut event handler tries to commit an ember-data object.
If the user was editing a text field and clicked a button, both a focusOut event and a click event
will be fired. If both of these handlers try to modify and commit the same ember-data object, the
object will be in-flight and the second handler will fail.
*/
function waitForKeyTrue(obj, key, fn) {
var id = Date.now();
var observationFn = function() {
if (obj.get(key)) { return; }
obj.removeObserver(key, observationFn);
console.log('resuming', id);
Em.run.next(function() {
fn();
});
};
if (obj.get(key)) {
console.log('scheduling change for later', id);
obj.addObserver(key, observationFn);
} else {
console.log('running change immediately');
fn();
}
}
function whenDoneSaving(obj, fn) {
waitForKeyTrue(obj, 'isSaving', fn);
}
//--------------------------------------------------
_deletePlaceholder: function(event, view) {
var button = $(this);
whenDoneSaving(view.get('context'), function() {
button.closest('span.placeholder').remove();
view.appendSelectionSpace();
view.save();
});
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment