So, I'm trying to use jQuery deferred (and the fact that $.ajax returns a promise) to handle some asynchronous code.
Here is an example that shows what happens
var update_model = function(model, resp){ model.set('id', resp.id); m = model; }; var print_id = function(){ console.log(m.get('id')); }; var MyModel = Backbone.Model.extend({}); var m = new MyModel({title: 'test'}); var model_promise = m.save({author: 'me'}, {success: update_model}); $.when(model_promise).then(print_id);
The problem is that print_id is called BEFORE update_model , and I'm not sure how to do it the other way.
In the actual example, I'm trying to save anywhere from 1 to 100 models, and I need to get the identifiers from these models before I can move on to the next step.
Did I miss something basic here?
EDITupdate_model causes a call - this only happens after print_id . In addition, I tried using the done method on model_promise , and then using then :
model_promise.done(update_model).then(print_id);
But then update_model does not receive the necessary arguments.
source share