Overriding Extjs classes and invoking callParent

前端 未结 2 2048
广开言路
广开言路 2021-02-05 12:04

I have a few months of experience developing Extjs web application. I ran into this problem:

When I override a class, I modified the method and followed the previous imp

相关标签:
2条回答
  • 2021-02-05 12:18

    If you're using ExtJS 4.1.3 or later you can use this.callSuper(arguments) to "skip" the overridden method and call the superclass implementation.

    The ExtJS documentation for the method provides this example:

    Ext.define('Ext.some.Class', {
        method: function () {
            console.log('Good');
        }
    });
    
    Ext.define('Ext.some.DerivedClass', {
        method: function () {
            console.log('Bad');
    
            // ... logic but with a bug ...
    
            this.callParent();
        }
    });
    
    Ext.define('App.patches.DerivedClass', {
        override: 'Ext.some.DerivedClass',
    
        method: function () {
            console.log('Fixed');
    
            // ... logic but with bug fixed ...
    
            this.callSuper();
        }
    });
    

    And comments:

    The patch method cannot use callParent to call the superclass method since that would call the overridden method containing the bug. In other words, the above patch would only produce "Fixed" then "Good" in the console log, whereas, using callParent would produce "Fixed" then "Bad" then "Good"

    0 讨论(0)
  • 2021-02-05 12:24

    You can't use callParent but you can just call the grandparent class method directly instead.

    GrandparentClass.prototype.finishedLayout.apply(this, arguments);
    

    Here's a more generic (if somewhat fragile) approach.

    this.superclass.superclass[arguments.callee.$name].apply(this, arguments);
    
    0 讨论(0)
提交回复
热议问题