简体   繁体   English

如何用继承的类修改类的方法?

[英]How to modify a method of a class with a inherited class?

I want to modify this line self.drawer.toggle(e === 'graph') by self.drawer.toggle(e !== 'form') of the method "start" of this class: 我想通过此类的“开始”方法的self.drawer.toggle(e!=='form')修改这一行self.drawer.toggle(e ==='graph')

instance.web.SearchView = instance.web.Widget.extend({
    template: "SearchView",
    start: function() {
        var self = this;
        var p = this._super();
        this.$view_manager_header = this.$el.parents(".oe_view_manager_header").first();
        this.setup_global_completion();
        this.query = new my.SearchQuery()
                .on('add change reset remove', this.proxy('do_search'))
                .on('change', this.proxy('renderChangedFacets'))
                .on('add reset remove', this.proxy('renderFacets'));
        if (this.options.hidden) {
            this.$el.hide();
        }
        if (this.headless) {
            this.ready.resolve();
        } else {
            var load_view = instance.web.fields_view_get({
                model: this.dataset._model,
                view_id: this.view_id,
                view_type: 'search',
                context: this.dataset.get_context(),
            });

this.alive($.when(load_view)).then(function (r) { self.fields_view_get.resolve(r); return self.search_view_loaded(r); }).fail(function () { self.ready.reject.apply(null, arguments); }); } var view_manager = this.getParent(); while (!(view_manager instanceof instance.web.ViewManager) && view_manager && view_manager.getParent) { view_manager = view_manager.getParent(); } if (view_manager) { this.view_manager = view_manager; view_manager.on('switch_mode', this, function (e) { self.drawer.toggle(e === 'graph'); }); } return $.when(p, this.ready); },

Have I to overwrite all lines in my new class or is there another way? 我是否要覆盖新类中的所有行,或者还有另一种方法?

I would suggest that the best idea would be to allow your class's constructor to that name as an argument. 我建议最好的主意是允许您的类的构造函数以该名称作为参数。

I'm not familiar with the library you're using, but in terms of classical inheritance your class would look like this. 我不熟悉您使用的库,但是就经典继承而言,您的类看起来像这样。

class SearchView extends Widget {
  constructor(name='graph') {
    this.name = name;

    view_manager.on('switch_mode', this, function (e) {
      self.drawer.toggle(e === this.name);
    });
  }
}

By default this would create instances that used 'graph' , if you wanted an instance that used 'form' instead, then you would do the following: 默认情况下,这将创建使用'graph'的实例,如果您想要使用'form'的实例,则可以执行以下操作:

var searchView = new SearchView('form');

You wouldn't need to subclass it. 您无需对其进行子类化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM