Search code examples
javascriptbackbone.jsunderscore.jsbackbone-events

Backbone Model: Is there a way to differentiate the inital save event from subsequent ones?


The title of the question pretty much sums it up, I'd like my view to respond differently to a model instances initial save vs any future saves. Right now I'm grabbing the model's isNew attr before I save and then triggering a custom event, but I was wondering if there was anything built in?


Solution

  • With help from this answer, I came up with the following solution:

    var originalSync = Backbone.sync;
    Backbone.sync = function(method, model, options) {
        console.log(method);
        originalSync.apply(Backbone, [method, model, options]);
    };
    

    I can now check what method is being called.