Created
September 13, 2012 21:12
-
-
Save focusaurus/3717688 to your computer and use it in GitHub Desktop.
Make backbone.js friendly for use with async.js
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
//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