Overriding Extjs classes and invoking callParent

前端 未结 2 2047
广开言路
广开言路 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"

提交回复
热议问题