未定义的功能

时间:2013-01-10 18:40:06

标签: javascript

我对JavaScript非常陌生,我需要一些建议,说明为什么我收到一条错误消息,指出this.nodeundefined

我所拥有的是使用MVC架构师在ExtJs上构建的应用程序。在我的Controller中,我开发了一个包含树和网格的布局。在树中,可以通过单击“新建”按钮添加父节点,并且其方法可以正常工作。

但是,使用与用于插入记录相同的表单从我的数据库中id更新记录的方法正在生成一条消息,指出this.nodeundefined

这是我Edit handler的代码:

handleBtnEdit: function (btn, ev, eOpts) {
    console.log(this);
    var id = parseInt(this.node.get("id")), rec = this.app.getStore("ProblemRequirements").getById(id);
    this.launchForm(rec);
}, 
handleBtnAdd: function (btn, ev, eOpts) {
    var rec = Ext.create("SPOT.model.ProblemRequirement");
    this.launchForm(rec);
}, 
handleBtnSave: function (btn, eOpts) {
    var win = btn.up("window"), pnl = win.down("form"), frm = pnl.getForm(), grid = pnl.down("grid"), store = grid.getStore(), rec = frm.getRecord();
    if (frm.isValid()) {
        rec.set(frm.getValues());
        win.setLoading("Saving, please wait...");
        rec.save({
            callback : function(rec, operation) {
                if (operation.success) {
                    win.close();
                    this.getStore("ProblemRequirements").load();
                    this.playMsg("Problem requirement successfully " + (operation.action === "update" ? "updated" : "created" ) + ".");
                } else {
                    win.setLoading(false);
                    Ext.Msg.alert("Error!", operation.error[0].ERROR);
                }
            },
            scope : this
        });
    }
}, 
launchForm: function (rec) {
    Ext.create("Ext.window.Window", {
        buttons : [{
            handler : this.handleBtnSave,
            scope : this,
            text : "Save"
        }, {
            handler : this.handleBtnCancel,
            scope : this,
            text : "Cancel"
        }],
        closable : false,
        draggable : false,
        iconCls : (rec.phantom ? "icon-add" : "icon-edit"),
        items : [{
            listeners : {
                afterrender : {
                    fn : function(pnl, eOpts) {
                        pnl.getForm().loadRecord(rec);
                    },
                    scope : rec
                }
            },
            xtype : "problemrequirementForm"
        }],

        modal : true,
        resizable : false,
        title : (rec.phantom ? "Create a New" : "Edit") + " Problem Requirement",
        width : 500
    }).show();
},

2 个答案:

答案 0 :(得分:2)

node超出了范围。您需要确定在node中定义的范围并适当地引用它,或者您需要使其全局可见,或者您需要在本地(this)范围内定义它。

考虑这个人为的例子:

// Set global window var 'a'
window.a = 'foo';

// Create an object which has it's own scope:
var x = {
    a:'bar',

    speak:function(scope){
        console.log(scope['a']);
    }
};

然后你可以这样做:

// Have x report on its own scope (in function 'speak' this is the same
// as doing: console.log(this['a'])
x.speak(x);

> bar

// Set the scope to the (global) 'window' scope
x.speak(window);

> foo

现在,如果我们重新定义x,使其没有成员a,并尝试相同的事情:

var x = {
    speak:function(scope){
        console.log(scope['a']);
    }
};

x.speak(window);

> foo

x.speak(x);

> undefined

在上一个示例中,a对象的范围内没有x,因此结果为undefined。它无法在全局范围内看到a,因为它只显式检查本地(this)范围。

显然,这是一个愚蠢的例子,但希望它有所帮助。

干杯

答案 1 :(得分:2)

用此版本替换handleBtnEdit并复制粘贴控制台日志。

handleBtnEdit: function (btn, ev, eOpts) {
    console.log("-- Start of handleBtnEdit --");

    console.log(btn);
    console.log("-- 1 --");

    console.log(ev);
    console.log("-- 2 --");

    console.log(this);
    console.log("-- 3 --");

    console.log(this.getAttribute("id"));
    console.log("-- 4 --");

    console.log(this.node);
    console.log("-- 5 --");

    //var id = parseInt(this.node.get("id"));
    //var rec = this.app.getStore("ProblemRequirements").getById(id);
    //this.launchForm(rec);
}, 

另外,添加调用handleBtnEdit

的代码

<强>更新

控制台日志:

-- Start of handleBtnEdit -- 
btn :: Object { disabled= false , iconCls= "icon-edit" , id= "btn-edit" , more...} 
-- 1 -- 
ev :: Object { browserEvent=Event click, type= "click" , button= 0 , more...} 
-- 2 -- 
this :: Object { application={...}, id= "ProblemRequirements" , hasListeners={...}, more...} 
-- 3 --

因此,参数在id= "btn-edit"的按钮中传递。 this似乎是Object id= "ProblemRequirements"

更新2

由于this内没有节点且id似乎不是我们需要的节点,请尝试以下代码:

handleBtnEdit: function (btn, ev, eOpts) {
    var id = parseInt(btn.id);
    var rec = this.application.getStore("ProblemRequirements").getById(id);
    this.launchForm(rec);
}, 
相关问题