【问题标题】:Extjs4 class listeners and functionsExtjs4 类监听器和函数
【发布时间】:2014-08-26 12:24:23
【问题描述】:

在这段代码中,有很多现有的函数,但我必须开始使用这些函数制作一些扩展的 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 () {
    // ...
}

【问题讨论】:

  • 不太确定您遇到问题的原因。只需调用另一个函数。
  • 它说:“randomOtherFunction 不是一个函数” - 但现在我有了一个想法,尝试一下,如果它有效,我会回来告诉它...... :)

标签: extjs extjs4


【解决方案1】:

是的,它有效! :) ...我希望在旧的其他功能转到对象之前会很好。

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

【讨论】:

  • 我真的看不出这个答案对其他人有什么帮助,因为您遗漏了很多细节,所以似乎对您的情况非常具体。可能最好删除问题。
  • 我无法删除它:“对不起,这个问题有答案,无法删除;请标记它以引起版主注意。”
【解决方案2】:

您必须检查范围并使用它来调用处理函数。 试试下面的

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 () {
    // ...
}

希望对你有帮助。

【讨论】:

  • 函数是在类外显式定义的,所以这行不通。
  • 您必须向按钮添加一个事件并在处理程序中触发它。在另一个类中,您必须监听该事件并调用特定方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-10
  • 1970-01-01
  • 1970-01-01
  • 2013-12-24
  • 2014-12-13
  • 2014-02-21
  • 1970-01-01
相关资源
最近更新 更多