javascript 覆盖主干模型中的 fetch() 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10427086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:51:27  来源:igfitidea点击:

Overriding fetch() method in backbone model

javascriptbackbone.js

提问by Drejc

I would like to override the default fetch() method in a Backbone model, thus calling it only when needed.

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

Something like this:

像这样的东西:

Account.Check = Backbone.Model.extend({
    model : Account.Item,

    url : Settings.Url.checkAccount,

    fetch : function(options) {         
                if (someCondition()) {
                    // do some stuff
            } else {
               super.fetch(options);
                }
    }
});

My question is how to provide the same behaviour as the default fetch() method in the // do some other stuffpart?

我的问题是如何在// do some other stuff部分提供与默认 fetch() 方法相同的行为?

回答by alex

This should do it...

这个应该可以...

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