Extjs 如何在扩展时初始化新元素 - 不丢失范围

发布于 2024-09-07 11:51:37 字数 657 浏览 4 评论 0原文

我试图更好地扩展 Extjs 的类,而我的发展导致我遇到了这个问题:

我扩展了 Ext.Panel 并且我希望我的扩展底部工具栏默认带有一个按钮。

myPanel = Ext.extend(Ext.Panel, {
    method: function () {
        return 'response!';
    },

    bbar: new Ext.Toolbar({
        items:
        [
            {
                xtype: 'button',
                text: 'Hit me!',
                handler: function (button, event) {
                    alert(this.method());
                },
                scope: this
            }
        ]
    })
});

我还不知道为什么这是不允许的。 this 指向全局范围,而不是我的扩展面板 - 因此 .method() 在处理函数内是 undefined

I am trying to get better at extending the classes of Extjs, and my evolvement have lead me to this problem:

I have extended an Ext.Panel and I want my extension to have a bottom toolbar with one button as default.

myPanel = Ext.extend(Ext.Panel, {
    method: function () {
        return 'response!';
    },

    bbar: new Ext.Toolbar({
        items:
        [
            {
                xtype: 'button',
                text: 'Hit me!',
                handler: function (button, event) {
                    alert(this.method());
                },
                scope: this
            }
        ]
    })
});

What I haven't learnt yet is why this is not allowed. this is pointing at the global scope and not my extended panel - thus .method() is undefined inside the handler function.

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

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

发布评论

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

评论(2

迷路的信 2024-09-14 11:51:37

您在原型上而不是在特定对象上定义 bbar。

重写 initComponent 并将 bbar 定义移至其中。

myPanel = Ext.extend(Ext.Panel, {
    method: function () {
        return 'response!';
    },

    initComponent: function() {    
        var bbar = new Ext.Toolbar({
            items:
            [
                {
                    xtype: 'button',
                    text: 'Hit me!',
                    handler: function (button, event) {
                        alert(this.method());
                    },
                    scope: this
                }
            ]
        });

        // Config object has already been applied to 'this' so properties can 
        // be overriden here or new properties (e.g. items, tools, buttons) 
        // can be added, eg:
        Ext.apply(this, {
            bbar: bbar
        });

        // Call parent (required)
        myPanel.superclass.initComponent.apply(this, arguments);

        // After parent code
        // e.g. install event handlers on rendered component
    }
});

请参阅http://www.sencha.com/learn/Manual:Component:Extending_Ext_Components 扩展组件时可以使用的模板

You're defining the bbar on the prototype rather than on a specific object.

Override initComponent and move the bbar definition inside it.

myPanel = Ext.extend(Ext.Panel, {
    method: function () {
        return 'response!';
    },

    initComponent: function() {    
        var bbar = new Ext.Toolbar({
            items:
            [
                {
                    xtype: 'button',
                    text: 'Hit me!',
                    handler: function (button, event) {
                        alert(this.method());
                    },
                    scope: this
                }
            ]
        });

        // Config object has already been applied to 'this' so properties can 
        // be overriden here or new properties (e.g. items, tools, buttons) 
        // can be added, eg:
        Ext.apply(this, {
            bbar: bbar
        });

        // Call parent (required)
        myPanel.superclass.initComponent.apply(this, arguments);

        // After parent code
        // e.g. install event handlers on rendered component
    }
});

See http://www.sencha.com/learn/Manual:Component:Extending_Ext_Components for a template you can use when extending components

ぽ尐不点ル 2024-09-14 11:51:37

您必须记住,作为 items 数组的第一个元素的匿名对象是在与 Ext.extend(... 的范围相同的范围内创建的) > 被执行,

如果您有这样的情况:

var o = { 'a': a, 'b': b, scope: this };

您会期望 oaobo.scope 具有与 相同的值。当前作用域中的 >abthis 在这里,情况有点复杂,因为您在创建数组的同时创建对象。对象等,但推理是相同的,

您应该做的是在构造函数中定义 this.bbar

myPanel = Ext.extend(Ext.Panel, {
    method: function () {
        return 'response!';
    },

    constructor: function(config) {
        this.bbar = new Ext.Toolbar({
            items:
            [
                {
                    xtype: 'button',
                    text: 'Hit me!',
                    handler: function (button, event) {
                        alert(this.method());
                    },
                    scope: this
                }
            ]
        });

        myPanel.superclass.constructor.apply(this, arguments);
    }
});

You have to keep in mind that the anonymous object that is the first element of the items array is created in the same scope as the one in which Ext.extend(... is executed.

If you had this:

var o = { 'a': a, 'b': b, scope: this };

you would expect that o.a, o.b, and o.scope would have the same values as a, b, and this in the current scope. Here, it's a little more complex because you are creating an object while creating an array while creating an object, etc., but the reasoning is the same.

What you should do instead is define this.bbar inside the constructor:

myPanel = Ext.extend(Ext.Panel, {
    method: function () {
        return 'response!';
    },

    constructor: function(config) {
        this.bbar = new Ext.Toolbar({
            items:
            [
                {
                    xtype: 'button',
                    text: 'Hit me!',
                    handler: function (button, event) {
                        alert(this.method());
                    },
                    scope: this
                }
            ]
        });

        myPanel.superclass.constructor.apply(this, arguments);
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文