简体   繁体   English

在Extjs中动态扩展类

[英]Extending class dynamically in Extjs

I need to extend class dynamically and use this code: 我需要动态扩展类并使用以下代码:

Calc.grid.Table["Table"+key] = function(config) {
        config = config || {};
        Ext.applyIf(config,{
                id: 'calc-grid-table'+key
                ,baseParams: { 
                    action: 'mgr/calc/calcGetTable'+key
                    ,query: 'Calc'+key 
                }
        });
        Calc.grid.Table["Table"+key].superclass.constructor.call(this,config)
};
Ext.extend(Calc.grid.Table["Table"+key],Calc.grid.Table);
Ext.reg('calc-grid-table'+key,Calc.grid.Table["Table"+key]);

but it gives me an error: 但这给了我一个错误:

Uncaught TypeError: Cannot read property 'superclass' of undefined

I tried to change it to this 我试图将其更改为此

this.superclass.constructor.call(this,config)

but error is 但是错误是

Uncaught SyntaxError: Unexpected identifier

The problem is in dynamic literals. 问题出在动态文字上。 When I do like this it works fine 当我这样做时,效果很好

Calc.grid.Table.Table21 = function(config) {
        config = config || {};
        Ext.applyIf(config,{
                id: 'calc-grid-table'+21
                ,baseParams: { 
                    action: 'mgr/calc/calcGetTable'+21
                    ,query: 'Calc'+21
                }
        });
        Calc.grid.Table.Table21.superclass.constructor.call(this,config)
};
Ext.extend(Calc.grid.Table.Table21,Calc.grid.Table);
Ext.reg('calc-grid-table'+21,Calc.grid.Table.Table21);

How can I fix it? 我该如何解决? What am I doing wrong? 我究竟做错了什么?

Thanx in advance. 提前感谢。

You're key variable being used in your constructor isn't scoped to that function, so it is possible that is can be changed before the constructor is called. 您在构造函数中使用的key变量没有作用于该函数,因此可以在调用构造函数之前对其进行更改。 You can use Ext's createDelegate to tie the variable to the function so that it can't change on you: 您可以使用Ext的createDelegate将变量绑定到函数,以使它不会在您身上改变:

Calc.grid.Table["Table"+key] = function(key, config) {
    config = config || {};
    Ext.applyIf(config,{
            id: 'calc-grid-table'+key
            ,baseParams: { 
                action: 'mgr/calc/calcGetTable'+key
                ,query: 'Calc'+key 
            }
    });
    Calc.grid.Table["Table"+key].superclass.constructor.call(this,config)
}.createDelegate(this, [key], 0);

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

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