Backbone JS Models save() – always() Callback

A few days ago I had an enlightenment regarding BackboneJS’s save() method.

What I wanted to do is to set a .always() (or completed callback, depends on which JQuery version you are) for a certain model save() method.

The documentation of Backbone states the following:

save accepts success and error callbacks in the options hash

Following the JQuery ajax() convention, I was certain that there is also a completed callback which can be set and be called whether the call was success or error. I found that there is nothing like that and wondered: “Is it possible they missed it?”

I was just going to waste a lot of digital ink and post a question in Stack Overflow, but luckily a more thorough look at the documentation yielded the solution:
Apparently .save() calls Backbone’s .sync() which return the jqXHR object of the ajax request produced by the .save() call. This allows you to do whatever you wish with that call, including .always() callbacks with:

I felt really stupid missing it, and I’d be more than glad to know I’m not the only one – please share your own most-obvious-things-missed, I’m sure it’ll bring a smile on some other people faces as well 🙂


3 thoughts on “Backbone JS Models save() – always() Callback

  1. We passed trough the same situation ones we were dealing with backbone save , and what we did was evaluate the success or error using the response from backend servers , and notify everyone that is listening the current model whit two calls , success or error. In my opinion you can achive the ‘always/complete’ method whit this:

    var self = this;{}, {
    success: function (model, response, options) {
    if (response && response.success) {
    self.trigger(‘sucess’, model, response, options);
    } else {
    self.trigger(‘error’, model, response, options);
    error: function (model, response, options) {
    self.trigger(‘error’, model, response, options);

  2. @Snidely: this is really interesting comment I need to take into count. Fortunately for me it does not matter since I am using only the form validations triggered before the is invoked. I think you could call model.validate manually and perform the adequote tasks before you call afterwards (e.g. if after manually called model.validate there are validation errors – enable the fields and do nothing else otherwise call

    @Author: I am definitely missing this .always or .complete AJAX callbacks in Backbone save/sync methods and find it very stupid! Thanks for excellent research and solution!

  3. Except it only returns that jqXHR object in the case of Backbone model validation success. If model validation fails, you get `false`, which almost defeats the purpose. :-/
    For example, in my case I’m disabling form submit buttons, then issuing a ``, but whether or not validation passes (server or client), I need to re-enable the buttons.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s