1

Hi I have a base Model -

var BaseModel = Backbone.Model.extend({

            initialize : function(input){
                // Some base initialization implementation common for all Concrete Models
            }, 
            ajaxCall : function(input){

                var dfd = new jQuery.Deferred();
                $.ajax({
                    type: 'Get',
                    url: input.url,
                    success:function(data){

                        // Some on success implementation   
                        dfd.resolve(data);
                    },
                    error:function(){
                        dfd.reject();
                    }
                });

                return dfd.promise();
            }

});

Now, I want to create a ConcreteModel that extends the BaseModel's fetch function and just overrides the ajax success

var ConcreteModel = BaseModel.extend({

            ajaxCall : function(input){

                   BaseModel.prototype.ajaxCall.call(this, input);

                   // How to override just the ajax success implementation
            }

});

How do I just override the ajax success implementation in the ajaxCall function of ConcreteModel.

Thanks.

Student
  • 4,481
  • 8
  • 27
  • 32

2 Answers2

0

You could pass an option second parameter to ajaxCall(input, success) and then within the ajax call have:

$.ajax({
    type: 'Get',
    url: input.url,
    success: $.isFunction(success) ? success : function(data){

        // Some on success implementation   
         dfd.resolve(data);
    },
    error:function(){
        dfd.reject();
    }
});

Or you could store the ajax request

this.ajax = $.ajax({...});

And then overwrite it in the ConcreteModel

this.ajax.success(function(){});
ArrayKnight
  • 6,956
  • 4
  • 17
  • 20
0

You can define ajax call successful callback as one of the Base Model method:

var BaseModel = Backbone.Model.extend({

    initialize : function(input){
        // Some base initialization implementation common for all Concrete Models
    }, 
    ajaxCall : function(input){
        var dfd = new jQuery.Deferred();
        $.ajax({
            type: 'Get',
            url: input.url,
            success:this.onSuccess,
            error:function(){
                dfd.reject();
            }
        });

    return dfd.promise();
    },
    onSuccess: function(data){
        // Some on success implementation   
        dfd.resolve(data);
    }
});

Then just override the implementation in the onSuccess function of ConcreteModel

var ConcreteModel = BaseModel.extend({
    onSuccess: function(data){
        alert("hello from concrete mode: "+data);
    }
});    
Chickenrice
  • 5,727
  • 2
  • 22
  • 21