Veka0881
Veka0881

Reputation: 170

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.

Upvotes: 0

Views: 446

Answers (1)

Vsevolod Goloviznin
Vsevolod Goloviznin

Reputation: 12324

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)

Upvotes: 1

Related Questions