首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ext.bind在ExtJS中看不到函数

Ext.bind在ExtJS中看不到函数
EN

Stack Overflow用户
提问于 2014-07-23 14:48:55
回答 1查看 3.5K关注 0票数 0

我正在尝试修改Portlet示例并将其移植到我们的程序中。代码如下所示:

代码语言:javascript
复制
Ext.create('Ext.container.Viewport',{
            id: 'app-viewport', //creates viewport
            layout: {
                type: 'border',
                padding: '0 5 5 5' // pad the layout from the window edges
            },

            onPortletClose: function(portlet) {
                this.showMsg('"' + portlet.title + '" was removed');
            },

            showMsg: function(msg) {
                var el = Ext.get('app-msg'),
                    msgId = Ext.id();

                this.msgId = msgId;
                el.update(msg).show();

                Ext.defer(this.clearMsg, 3000, this, [msgId]);
            },

            clearMsg: function(msgId) {
                if (msgId === this.msgId) {
                    Ext.get('app-msg').hide();
                }
            },

            items: [{
                id: 'app-header',
                xtype: 'box',
                region: 'north',
                height: 40,
                html: 'Ext Welcome'
            },{
                xtype: 'container',
                region: 'center',
                layout: 'border',
                items: [{
                    id: 'app-options', //Creates the Options panel on the left
                    title: 'Options',
                    region: 'west',
                    animCollapse: true,
                    width: 200,
                    minWidth: 150,
                    maxWidth: 400,
                    split: true,
                    collapsible: true,
                    layout:{
                        type: 'accordion',
                        animate: true
                    },
                    items: [{
                        html: content,
                        title:'Navigation',
                        autoScroll: true,
                        border: false,
                        iconCls: 'nav'
                    },{
                        title:'Settings',
                        html: content,
                        border: false,
                        autoScroll: true,
                        iconCls: 'settings'
                    }]
                },{
                    id: 'app-portal', //Creates the panel where the portal drop zones are.
                    xtype: 'mainpanel',
                    region: 'center',
                    items: [{
                        id: 'col-1', //Each column represents a drop zone column. If we add more there are more created and width gets adjusted accordingly
                        items: [{
                            id: 'portlet-1',
                            title: 'Portlet 1',
                            html: content,
                            listeners: {
                                'close': Ext.bind(this.onPortletClose, this)
                        },{
                            id: 'portlet-2',
                            title: 'Portlet 2',
                            html: content,
                            listeners: {
                                'close': Ext.bind(this.onPortletClose, this)
                            }
                        }]
                    },{
                        id: 'col-2',
                        items: [{
                            id: 'portlet-3',
                            title: 'Portlet 3',
                            html: content,
                            listeners: {
                                'close': Ext.bind(this.onPortletClose, this)
                            }
                        }]
                    },{
                        id: 'col-3',
                        items: [{
                            id: 'portlet-4',
                            title: 'Portlet 4',
                            html: content,
                            listeners: {
                                'close': Ext.bind(this.onPortletClose, this)
                            }
                        }]
                    }]
                }]
            }]
        });

问题是Ext.bind无法读取onPortletClose函数,浏览器给我一个:

Uncaught TypeError: Cannot read property 'apply' of undefined误差我检查了堆栈,基本上在Ext.bind(fn,fn )中,fn是未定义的,因此它无法读取处理程序函数。我的应用程序和示例中的不同之处在于,我将其直接添加到JSP的Ext.onReady()中,而在示例中,所有这些都是通过Ext.apply(this, {...})添加的。我真的很困惑。我尝试了各种噱头来强制视口上的作用域,但似乎Ext.bind()内部的任何东西都失去了与外部或其他东西的联系。我以前使用过Ext.bind(),虽然它在initComponent配置中,但是它运行得很好。这是强制性的吗?如果没有,那有什么问题呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-23 16:01:53

在这里理解this的含义是很重要的(或者当使用JavaScript或ExtJS时)。

在全局上下文中,this引用全局对象,也称为全局window变量。Ext.onReady函数中的情况是这样的:

代码语言:javascript
复制
Ext.onReady(function() {
    console.log(this === window);
    // -> true
    // because this is the global object
});

在对象上下文中,默认情况下, this引用函数的所有者对象(有绕过它的方法,实际上这正是您希望使用Ext.bind实现的):

代码语言:javascript
复制
var obj = {
   prop: 'My text property',
   fn: function() {
       console.log(this.prop);
       // -> My text property
       // because "this" refers to our object "obj"
   }
};

现在,这正是您的案例与示例中的案例的区别所在。

在使用Ext.onReady this的方法时--正如上面所指出的--将引用全局对象,该对象当然没有函数onPortletClose

当使用示例中的方法时,可以从this内部访问initComponent,这是派生视图端口类的成员函数,因此this引用组件实例(=所属对象),该实例允许您访问函数。

代码语言:javascript
复制
Ext.define('MyViewport', {
    extend: 'Ext.container.Viewport',

    onPortletClose: function(portlet) {
        this.showMsg('"' + portlet.title + '" was removed');
    },

    initComponent: function() {
        console.log(this);
        // -> the instance of your viewport class

        Ext.apply(this, {
            items:[{
                xtype: 'panel',
                title: 'Portlet 1',
                listeners: {
                    'close': Ext.bind(this.onPortletClose, this)
                }
            }]
        });

        this.callParent();
    }
});

旁注

Ext.apply

Ext.apply实际上只是将所有属性从一个对象复制到另一个对象,因此在本例中,它只是一个函数调用将该对象的所有属性应用于this的方便方法。你也可以用:

代码语言:javascript
复制
this.items = [...];

更改侦听器的范围

在向组件添加侦听器时不需要使用Ext.bind,只需在listeners对象中提供scope配置:

代码语言:javascript
复制
listeners: {
    close: function() {...},
    scope: this
}

外部来源

如果您想深入到更深的地方(我建议这样做),您可能想在MDN (Mozilla开发人员网络)上读到更多关于这一点的内容。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24913743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档