在主干模型中重写 fetch() 方法

Overriding fetch() method in backbone model

本文关键字:fetch 方法 重写 模型      更新时间:2023-09-26

我想覆盖 Backbone 模型中的默认 fetch() 方法,从而只在需要时调用它。

像这样:

Account.Check = Backbone.Model.extend({
    model : Account.Item,
    url : Settings.Url.checkAccount,
    fetch : function(options) {         
                if (someCondition()) {
                    // do some stuff
            } else {
               super.fetch(options);
                }
    }
});

我的问题是如何在 //做一些其他事情部分提供与默认 fetch() 方法相同的行为?

这应该可以做到...

fetch : function(options) {         
           if (someCondition()) {
              // do some stuff
           } else {
              this.constructor.__super__.fetch.apply(this, arguments);
              // Or (less flexible)
              Backbone.Model.prototype.fetch.apply(this, arguments);    
           }
         }