按钮处理程序中的扩展 ExtJS 对象属性

发布于 2024-12-01 09:01:30 字数 2071 浏览 1 评论 0原文

我确实很难从表单上的按钮处理程序中访问作为扩展 FromPanel 的属性的变量。如果有人可以提供帮助,我将不胜感激。

我尝试从 updateUnitsButton 的处理程序中访问 myAttribute 的示例表单:

TrainOverviewPanel = Ext.extend(Ext.FormPanel, {
    myAttribute: undefined
    ,initComponent:function() {
        var unitIdField1 = new Ext.form.TextField({
            itemId: 'unitIdField1',
            flex: 1
        });
        var unitIdField2 = new Ext.form.TextField({
            itemId: 'unitIdField2',
            flex: 1
        });
        var unitIdField3 = new Ext.form.TextField({
            itemId: 'unitIdField3',
            flex: 1
        });
        var unitIdFields = new Ext.form.CompositeField({
            itemId: 'unitIdFields',
            items: [unitIdField1, unitIdField2, unitIdField3],
            width: 200
        });

        var updateUnits = function() {
            alert("How can I access attribute: " + this.myAttribute);
        }
        var updateUnitsButton = new Ext.Button({
            tdoId: undefined,
            text: 'Update',
            handler: updateUnits,
            width: 50
        });

        var updatableUnitIdFields = new Ext.form.CompositeField({
            readOnly: false,
            fieldLabel: 'Unit ID',
            itemId: 'updatableUnitIdFields',
            items: [unitIdFields, updateUnitsButton],
            width: 300
        });

        Ext.apply(this, {
            width: 450,
            height: 130,
            margins:'10 0 0 0',
            items:  [ updatableUnitIdFields ]
        });
        TrainOverviewPanel.superclass.initComponent.apply(this, arguments);
    }

    ,onRender:function() {
        TrainOverviewPanel.superclass.onRender.apply(this, arguments);
    }

    ,refresh: function(data) {
        this.myAttribute = data.myAttribute;
        var unitIdFields = this.getComponent('updatableUnitIdFields').items.get(0);
        unitIdFields.items.get(0).setValue(data.stockId1);
        unitIdFields.items.get(1).setValue(data.stockId2);
        unitIdFields.items.get(2).setValue(data.stockId3);
    }
});

I'm having real difficulty getting access to a variable that is an attribute of an extended FromPanel from within a button handler on the form. If anyone could offer assistance I'd greatly appreciate it.

Example form where I try to access myAttribute from within the handler of updateUnitsButton:

TrainOverviewPanel = Ext.extend(Ext.FormPanel, {
    myAttribute: undefined
    ,initComponent:function() {
        var unitIdField1 = new Ext.form.TextField({
            itemId: 'unitIdField1',
            flex: 1
        });
        var unitIdField2 = new Ext.form.TextField({
            itemId: 'unitIdField2',
            flex: 1
        });
        var unitIdField3 = new Ext.form.TextField({
            itemId: 'unitIdField3',
            flex: 1
        });
        var unitIdFields = new Ext.form.CompositeField({
            itemId: 'unitIdFields',
            items: [unitIdField1, unitIdField2, unitIdField3],
            width: 200
        });

        var updateUnits = function() {
            alert("How can I access attribute: " + this.myAttribute);
        }
        var updateUnitsButton = new Ext.Button({
            tdoId: undefined,
            text: 'Update',
            handler: updateUnits,
            width: 50
        });

        var updatableUnitIdFields = new Ext.form.CompositeField({
            readOnly: false,
            fieldLabel: 'Unit ID',
            itemId: 'updatableUnitIdFields',
            items: [unitIdFields, updateUnitsButton],
            width: 300
        });

        Ext.apply(this, {
            width: 450,
            height: 130,
            margins:'10 0 0 0',
            items:  [ updatableUnitIdFields ]
        });
        TrainOverviewPanel.superclass.initComponent.apply(this, arguments);
    }

    ,onRender:function() {
        TrainOverviewPanel.superclass.onRender.apply(this, arguments);
    }

    ,refresh: function(data) {
        this.myAttribute = data.myAttribute;
        var unitIdFields = this.getComponent('updatableUnitIdFields').items.get(0);
        unitIdFields.items.get(0).setValue(data.stockId1);
        unitIdFields.items.get(1).setValue(data.stockId2);
        unitIdFields.items.get(2).setValue(data.stockId3);
    }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟凡古楼 2024-12-08 09:01:30

您可以使用两种方法来完成此操作。

  • 第一个

使用闭包:

// ...
var me = this;
var updateUnits = function() {
    alert("How can I access attribute: " + me.myAttribute);
};
var updateUnitsButton = new Ext.Button({
    tdoId: undefined,
    text: 'Update',
    handler: updateUnits,
    width: 50
});
// ...
  • 第二个

通过将范围变量发送到处理程序:

// ...
var updateUnits = function() {
    alert("How can I access attribute: " + this.myAttribute);
};
var updateUnitsButton = new Ext.Button({
    tdoId: undefined,
    text: 'Update',
    //handler: updateUnits,
    width: 50
});
updateUnitsButton.on('click', updateUnits, this);
// ...

You can do it using two ways.

  • The first one

using closure:

// ...
var me = this;
var updateUnits = function() {
    alert("How can I access attribute: " + me.myAttribute);
};
var updateUnitsButton = new Ext.Button({
    tdoId: undefined,
    text: 'Update',
    handler: updateUnits,
    width: 50
});
// ...
  • The second one

by sending scope variable to handler:

// ...
var updateUnits = function() {
    alert("How can I access attribute: " + this.myAttribute);
};
var updateUnitsButton = new Ext.Button({
    tdoId: undefined,
    text: 'Update',
    //handler: updateUnits,
    width: 50
});
updateUnitsButton.on('click', updateUnits, this);
// ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文