Veronica Lotti Veronica Lotti - 1 month ago 23
Javascript Question

Backbone options to fetch a callback success

subscribe: function () {

this.model.save({email: email}, {
success: function (data) {
var msg = view.model.getSuccessMessage(view._SiteInstance.get('paid_features'));
view.notify({message: msg, timeout: 7000});
view.trigger('email:subscribed');
this.storageKey = 'email_subscribe';
localStorage.setItem(this.storageKey, true);
view.$el.parent().removeClass('slide-in');
};


}

This is what I need to override in order to show my confirm message into my pop up and not to call view.notify

My function belongs to a oneView.

A pop up form is calling myFunction (from another view).

I want to call myFunction from my view and override it (I need to override the success callback) with Backbone.

Answer

You can override your model's `save method and inject your success function there.

Backbone.Model.extend({

    // Overwrite save function
    save: function(attrs, options) {
        options || (options = {});
        attrs || (attrs = _.clone(this.attributes));
        var oldSuccess = options.success || function() {};

        options.success = function(result){
            //you code goes here
        }

        // Proxy the call to the original save function
        Backbone.Model.prototype.save.call(this, attrs, options);
    }
});

Overriding is taken from this question: Exclude model properties when syncing (Backbone.js)

Comments