Question

This is basically what the original problem reduces to:

I have a Backbone model and I want to execute a certain action every time it saved successfully, but not after it's fetched. The cleanest and least intrusive way to do this, as I see it, would be to attach a handler to the sync event and check the XHR object: if it's a response to a GET, do one thing, and another if it was a POST.

However, looks like I cannot determine the HTTP method the jqXHR was created in response to... or can I?

Was it helpful?

Solution

You can override the Backbone.sync method like this :

var sync = Backbone.sync;

Backbone.sync = function(method, model, options) { // override the Backbone sync

                    // override the success callback if it exists
                    var success = options.success;
                    options.success = function(resp) {
                    if (success) success(model, resp, options);
                        // trigger the event that you want
                        model.trigger(methodMap[method]);
                    };

                    sync.call(this, method, model, options);
                };

methodMap looks like :

var methodMap = {
'create': 'POST',
'update': 'PUT',
'patch':  'PATCH',
'delete': 'DELETE',
'read':   'GET'
}

So in order to catch the GET/POST method all you have to do is :

initialize: function() { // your view initialize
    this.listenTo(this.model, "GET", /* your GET callback */);
    this.listenTo(this.model, "POST", /* your POST callback */);
}

OTHER TIPS

You can override the save method to do whatever you want; something like this:

@MyApp.module "Entities", (Entities, App, Backbone, Marionette, $, _) ->

  class Entities.Model extends Backbone.Model

    save: (data, options = {}) ->
      ## do whatever you need to do ##
      super data, options

Then just extend your models from this definition instead of Backbone.Model, like so:

class Entities.MyModel extends App.Entities.Model
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top