Extjs4类侦听器和函数

Extjs4类侦听器和函数,extjs,extjs4,Extjs,Extjs4,在这段代码中,有许多现有函数,但我必须开始使用这些函数创建一些扩展的ExtJS类 如何将现有函数添加到类的侦听器 例如: Ext.define("My.Grid", { extend: 'Ext.grid.Panel', //... initComponent: function() { //... Ext.apply(this, { //... tbar: [{

在这段代码中,有许多现有函数,但我必须开始使用这些函数创建一些扩展的ExtJS类

如何将现有函数添加到类的侦听器

例如:

Ext.define("My.Grid", { 
    extend: 'Ext.grid.Panel',
    //...
    initComponent: function() {
        //...
        Ext.apply(this, {
            //...
            tbar: [{
                xtype: 'button',
                icon: 'img/x.png',
                handler: function(){
                   // need to call randomOtherFunction here
                }
            }]
        });
    }
});

function randomOtherFunction () {
    // ...
}

您必须检查作用域并使用它调用处理程序函数。 试试下面的方法

Ext.define("My.Grid", { 
    extend: 'Ext.grid.Panel',
    //...
    initComponent: function() {
        //...
        Ext.apply(this, {
            //...
            tbar: [{
                xtype: 'button',
                icon: 'img/x.png',
                scope:this,
                handler:this.randomOtherFunction
            }]
        });
    }
});

function randomOtherFunction () {
    // ...
}
希望它能帮助你。

是的,它很有效!:)。。。我希望,在旧的其他函数应用到对象之前,它将是好的

function randomOtherFunction () {
    // ...
}

My.functions = {
    randomOtherFunction: function () {
        randomOtherFunction();
    }
};

Ext.define("My.Grid", { 
    extend: 'Ext.grid.Panel',
    //...
    initComponent: function() {
        //...
        Ext.apply(this, {
            //...
            tbar: [{
                xtype: 'button',
                icon: 'img/x.png',
                handler: function(){
                    My.functions.randomOtherFunction();
                }
            }]
        });
    }
});

不太清楚你为什么会有问题。只需调用另一个函数。它说:“randomOtherFunction不是函数”-但现在我有了一个想法,尝试一下,我会返回并告诉它,如果它有效的话…:)函数是在类外部显式定义的,因此这不起作用。您必须向按钮添加一个事件并在处理程序中激发它。在另一节课上,你必须听那个事件并调用特定的方法。我真的不知道这个答案对其他人有什么帮助,因为你遗漏了太多细节,所以看起来非常具体。可能最好删除这个问题。我不能删除它:“对不起,这个问题有答案,不能删除;改为标记它以引起版主的注意。”