Adi
Adi

Reputation: 119

How to override existing Backbone Model method keeping other methods same

I have a backbone model which I don't have access to edit.So i want to modify the method of it, so that other methods functionality wont affect.

ShoppingDetail = Backbone.Model.extend({
   className: 'CartID',
   fetch: function() {},
   checkForChanges: function() {},
   newCoupon: function() {},
   saveAndallow: function() {}
 });

 shoppingDetailModel = new ShoppingDetail();
 shoppingCartView = new ShoppingCartView({
   model: shoppingDetailModel
 });

So i want to override saveAndallow method of the model.How could i do that without affecting other methods of that model

Upvotes: 2

Views: 587

Answers (1)

vharekal
vharekal

Reputation: 165

Why don't you create a new Model which will extend your ShoppingDetail Model ?

ShoppingDetail = Backbone.Model.extend({ 
    className: 'CartID', 
    fetch: function() {},
    checkForChanges : function() {},
    newCoupon: function(){},
    saveAndallow: function(){}
});

NewShoppingDetail = ShoppingDetail.extend({
    saveAndallow: function(){};
});

shoppingDetailModel = new NewShoppingDetail();
shoppingCartView =  new ShoppingCartView({
        model : shoppingDetailModel
});

So now, when you call any method, it will first check if it is available in NewShoppingDetail, if not available, then it will check ShoppingDetail.

Upvotes: 2

Related Questions