未捕获的TypeError:对象#<object>没有方法'apply'错误</object>

时间:2014-01-29 17:55:20

标签: javascript extjs4

单击“编辑”按钮时出现错误Uncaught TypeError: Object #<Object> has no method 'apply'。这是我的小组:

Ext.define(
    "ux.panel.MyPanel", {
    extend : "Ext.form.Panel",
    xtype : "MyPanel",
    title : "Bla bla title"

    initComponent : function () {
        ux.panel.MyPanel.superclass.initComponent.call(this);
    },
    init : function (initParameters) {
        this.initParameters = initParameters;

        this.removeAll(true);

        if (this.initParameters == null)
            return;
    },
    editSomething : function () {
        alert("editTV");
    },
    tbar : [{
            text : "Edit"
            iconCls : "silk-edit-16",
            id : "editbutton",
            disabled : false,
            listeners : {
                click : {
                    scope : this,
                    fn : this.editSomething
                }
            }
        }, {
            text : "Remove",
            iconCls : "silk-delete-16",
            id : "removeButton",
            disabled : false,
            listeners : {
                click : {
                    scope : this,
                    fn : function () {
                        alert("RemoveTV");
                    }
                }
            }
        }
    ]
});

此面板包含在由许多其他面板组成的页面中。我怀疑这个在this.editSomething并没有指向我的面板。我对如何解决这个问题一无所知。

有人知道原因吗?

1 个答案:

答案 0 :(得分:2)

这是因为this不是你所期望的那样。

tbar功能中添加initComponent,例如:

initComponent: function () {
    Ext.apply(this, {
        tbar: [{
            text: "Edit",
            iconCls: "silk-edit-16",
            id: "editbutton",
            disabled: false,
            listeners: {
                click: {
                    scope: this,
                    fn: this.editSomething
                }
            }
        }, {
            text: "Remove",
            iconCls: "silk-delete-16",
            id: "removeButton",
            disabled: false,
            listeners: {
                click: {
                    scope: this,
                    fn: function () {
                        alert("RemoveTV");
                    }
                }
            }
        }]
    });

    this.callParent(arguments);
}

http://jsfiddle.net/gkcU7/