javascript - 未定义的函数

标签 javascript

我对 JavaScript 很陌生,我需要一些建议来了解为什么我会收到一条错误消息,指出 this.node未定义

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

但是,使用与插入记录相同的形式从我的数据库中按 id 更新记录的方法会生成一条消息,说明 this.node未定义

这是我的编辑处理程序代码:

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();
},

最佳答案

用此版本替换您的 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 似乎是一个带有 id=“ProblemRequirements”Object

更新2

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

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

关于javascript - 未定义的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14264976/

相关文章:

php - 使用 php 服务器时间设置 Javascript 时间?

javascript - Angular 样板和 Twitter Bootstrap 3

javascript - React Native Input onEndEditing 奇怪的回调

JavaScript 动态对象

javascript - 在指定的时间间隔后第一次调用函数

javascript - 如何阻止 javascript 更改我的日期?

javascript - 如何添加一个类在到达特定的 div 时删除一个类(溢出隐藏)

javascript - Oracle JET CLI "ojet"未定义

javascript - 如何取消绑定(bind) keyup 事件?

javascript - 动态 .js 包含(使用 jQuery)会被认为是不好的还是最佳的做法?