extjs - 覆盖 Extjs 类并调用 callParent

标签 extjs extjs4 overriding

我有几个月的 Extjs Web 应用程序开发经验。我遇到了这个问题:

当我覆盖一个类时,我修改了该方法并遵循了之前的实现并调用了 callParent() .覆盖部分有效,但 callParent()调用旧的实现。

我的覆盖代码

Ext.override(Ext.layout.component.Draw, {
    finishedLayout: function (ownerContext) {

        console.log("new layouter being overriden");
        this.callParent(arguments);
    }
});

要覆盖的 Extjs 类方法:
finishedLayout: function (ownerContext) {
    var props = ownerContext.props,
        paddingInfo = ownerContext.getPaddingInfo();

    console.log("old layouter being overriden");
    this.owner.setSurfaceSize(props.contentWidth - paddingInfo.width, props.contentHeight - paddingInfo.height);

    this.callParent(arguments);
}

在控制台中,我可以看到新的布局器首先打印出消息,然后是旧的布局器实现......我放置了一个断点并回溯调用堆栈,callParent()新布局器的名称称为旧布局器。我需要调用父类,而不是重写的方法。

知道如何解决这个问题吗?

最佳答案

如果您使用的是 ExtJS 4.1.3 或更高版本,您可以使用 this.callSuper(arguments) “跳过”重写的方法并调用父类(super class)实现。

ExtJS documentation方法提供了这个例子:

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

和评论:

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"

关于extjs - 覆盖 Extjs 类并调用 callParent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15898565/

相关文章:

css - Bootstrap - 如何覆盖 css?

visual-studio - 使用 Entity Framework 时在实体中重写ToString,等于..

html - 水平翻转html和css

java - ExtJS 成功操作未被调用

javascript - 如何计算总时数?

javascript - 单击将文本框更改为文本区域

javascript - 如何在条形图中添加点击事件

extjs - Ext.js 4 面板上的滚动条

ExtJS 4 检查类是否存在

Javascript - 覆盖 console.log 并保留旧功能