如何覆盖现有的Backbone Model方法,保持其他方法相同

时间:2016-10-14 11:33:25

标签: javascript jquery model-view-controller backbone.js

我有一个骨干模型,我无权访问edit.So我想修改它的方法,以便其他方法功能不会影响。

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

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

所以我想覆盖模型的saveAndallow方法。我怎么能这样做而不影响该模型的其他方法

1 个答案:

答案 0 :(得分:2)

为什么不创建一个扩展ShoppingDetail模型的新模型?

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
});

现在,当您调用任何方法时,它将首先检查它是否在NewShoppingDetail中可用,如果不可用,则它将检查ShoppingDetail。