Skip to content

Instantly share code, notes, and snippets.

@focusaurus
Created September 13, 2012 21:12
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 focusaurus/3717688 to your computer and use it in GitHub Desktop.
Save focusaurus/3717688 to your computer and use it in GitHub Desktop.
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