微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript-extjs 4中的范围问题

我有这个treepanel,我想从“全部展开”按钮内部调用mainpaneltree的this.getId()方法,但是我得到的只是方法未定义.我试图将scope:this放在配置对象中,但没有成功.

Ext.define('MA.view.patient.Tree', {
extend : 'Ext.tree.Panel',
alias : 'widget.EditPatientTree',
title : 'Simple Tree',
width : 150,
store:'Tree',
dockedItems : [ {
    xtype : 'toolbar',
    items : [ {
        text : 'Expand All',
        scope: this,
        handler : function() {
    //this.expandAll gives "Uncaught TypeError: Object [object DOMWindow] has no method 'getId'"
            this.expandAll();
   //the same error for this.getId();
            this.getId();
        }
    } ]
} ],
rootVisible : false,
initComponent : function() {
    this.callParent(arguments);
}
});

所以我的问题是当您位于当前组件的嵌套方法或配置对象中时,如何获取当前组件的引用并调用方法

解决方法:

处理程序具有传入的参数,通常是按钮之一.从按钮可以获取容器.

Ext.define('MA.view.patient.Tree', {
extend : 'Ext.tree.Panel',
alias : 'widget.EditPatientTree',
title : 'Simple Tree',
width : 150,
store:'Tree',
dockedItems : [ {
    xtype : 'toolbar',
    items : [ {
        text : 'Expand All',
        scope: this,
        handler : function(button, event) {
            var toolbar = button.up('toolbar'), treepanel = toolbar.up('treepanel');
            treepanel.expandAll();
            treepanel.getId();
        }
    } ]
} ],
rootVisible : false,
initComponent : function() {
    this.callParent(arguments);
}
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐