Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Make backbone.js friendly for use with async.js
//Note, works with either Backbone.Model or Backbone.Collection
//Returns a function that will fetch the provided model and works with
//async's callback API
function asyncFetch(model) {
return function (callback) {
model.fetch({
success: function (model) {callback(null, model);},
error: function (model, response) {callback(response);}
});
};
}
//Returns a function that will save the provided model and works with
//async's callback API
function asyncSave(model) {
return function (callback) {
model.save(null, {
success: function (model) {callback(null, model);},
error: function (model, response) {callback(response);}
});
};
}
//Example usage
async.parallel([asyncSave(model1), asyncSave(model2), asyncFetch(someCollection)],
function (error) {
if (error) {
return self.displayError(error);
}
self.displaySuccess("Done!");
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.