Created
July 23, 2012 19:59
-
-
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 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
/* | |
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