Sencha extjs 属性范围

Sencha extjs attribute scope

本文关键字:范围 属性 extjs Sencha      更新时间:2023-09-26

目前我正在使用extjs 3.4,遇到了以下问题。我正在构造函数中创建 GridPanel,在设置此属性后,我设置了一个名为"bla"的变量名称,值为"test2",我提醒它以确保它已正确设置。这行得通!但是,当我的网格面板中按下按钮时,它无法再读取bla属性并说其"未定义"。

我跳过了代码中对问题没有意义的部分;)

请参阅下面的代码

    Ext.define('SC.view.WorkOrderHourGrid' ,{
    extend: 'Ext.grid.GridPanel',
    bla:"test",
    tbar: null,
    store:null,
    plugins:null,
    //Methods

    constructor:function(p_Store,config){
        this.tbar = this.buildTbar();
        this.bla = " test2";
        this.store = p_Store;
        //alerting bla attribute outputs test 2 !!
        alert(this.bla);

        var rowEditor = new Ext.ux.grid.RowEditor({
            saveText: 'Update'
        });
        this.plugins = [rowEditor];

        SC.view.WorkOrderHourGrid.superclass.constructor.call(this,config);
    },
    buildTbar:function()
    {
        return [{
            text: 'Toevoegen',
            handler: function(){
                tryOut();
            },
            //the button i was talking about
            handler:this.onAdd

        }];
    },
    onAdd:function(btn, ev)
    {
        //this outputs undefined !!!
        alert(this.bla);
    }

});

onAdd 函数中的范围是按钮。这就是为什么bla 是未定义的。

尝试像这样更改您的构建Tbar函数:

buildTbar:function()
{
    return [{
        text: 'Toevoegen',
        scope: this,
        //the button i was talking about
        handler:this.onAdd

    }];
},

我还没有测试过! :)