覆盖Extjs类并调用callParent [英] Overriding Extjs classes and invoking callParent

查看:92
本文介绍了覆盖Extjs类并调用callParent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



当我重写一个类时,我修改了该方法,并遵循上一个实现,并调用 callParent()。最重要的部分工作,但$ code> callParent()调用旧的实现。



我的覆盖代码

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

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

要覆盖的Extjs类方法:

  finishedLayout:function(ownerContext){
var props = ownerContext.props,
paddingInfo = ownerContext.getPaddingInfo();

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

this.callParent(arguments);
}

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



任何想法如何解决这个问题?

解决方案

如果您使用ExtJS 4.1.3或更高版本,您可以使用 this.callSuper(arguments)来跳过重写方法并调用超类实现。



ExtJS 文档提供了此示例:

  Ext.define('Ext.some.Class',{
method:function(){
console.log('Good');
}
}) ;

Ext.define('Ext.some.DerivedClass',{
method:function(){
console.log('Bad');

// ...逻辑但有一个错误...

this.callParent();
}
});

Ext.define('App.paches.DerivedClass',{
override:'Ext.some.DerivedClass',

方法:function(){
console.log('Fixed');

// ...逻辑但修复错误

this.callSuper();
}
});

和评论:


补丁方法不能使用callParent调用超类方法,因为这将调用包含错误的重写方法。换句话说,上面的补丁只会在控制台日志中产生固定,然后是好,而使用callParent会产生固定,那么坏然后是好



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 implementation and invoke callParent(). The overriding part works but the callParent() invoked the old implementation.

my overriding code

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

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

The Extjs class method to be overridden:

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

In the console, I can see that first the new layouter prints out the message followed by the old layouter implementation... I put a breakpoint and retrace the invocation stack, the callParent() of the new layouter called the old one. I need to call the parent class, but not the overridden method.

Any idea how to solve this problem?

解决方案

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.paches.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"

这篇关于覆盖Extjs类并调用callParent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆