Javascript:为什么在回调函数中无法获得变量?

时间:2018-05-08 10:30:29

标签: javascript function extjs callback

我已经阅读了几个不同的callback函数文档,但遗憾的是无法成功获得相关variable。我在这里缺少什么?

checkNo: function (callback) {
    var comboQuery = Ext.ComponentQuery.query('[name=foocombo]')[0].getSelectedRecord();
    var chkno = comboQuery.get('mychkno'); //Success to get this integer value

    callback(chkno); //Error raises here. Debugger says "callback is not a function"
    //if (callback) callback(chkno); //I've tried to check callback but did not work as well.
},

setFooCombo: function () {
    var me = this;
    var fooStore = Ext.getStore('fooComboStore');
    var chkno = ''; //Trying to pass an empty value. Not sure if correct approach 
    var checkno = me.checkNo(chkno); //Trying to get returned value from above function to be able using on url.

    fooStore.getProxy().setUrl(MyApp.Url() + '/foo/list?=' + checkno); //I need pass value that return from callback to here
    if (typeof checkno === MyApp.NUMBER) {
        fooStore.load();
    }


    // I've tried another way to set new URL as below but did not work too.
    // me.checkNo(function (checkno) {
    //fooStore.getProxy().setUrl(MyApp.Url() + '/foo/list?=' + checkno);
    //     if (typeof checkno === MyApp.NUMBER) {
    //         fooStore.load();
    //     }
    // });
},

更新:在Rahul Khandelwal的回答重新考虑了这些功能后,当然现在有效。

checkNo: function () {
    var comboQuery = Ext.ComponentQuery.query('[name=foocombo]')[0].getSelectedRecord();
    var chkno = comboQuery.get('checkno');

    return chkno;

},

setFooCombo: function () {
    var me = this;
    var fooStore = Ext.getStore('fooComboStore');
    var checkno = me.checkNo();

    fooStore.getProxy().setUrl(MyApp.Url() + '/foo/list?=' + checkno);
    if (typeof checkno === MyApp.NUMBER) {
        fooStore.load();
    }
},

2 个答案:

答案 0 :(得分:1)

调用该函数时,您没有使用回调功能。将其更改为正常的功能定义,如下所示。

CheckNo: function () {
var comboQuery = Ext.ComponentQuery.query('[name=foocombo]')[0].getSelectedRecord();
var chkno = comboQuery.get('mychkno'); //Success to get this integer value

return chkno; //Error raises here. Debugger says "callback is not a function"
//if (callback) callback(chkno); //I've tried to check callback but did not work as well.

}

要了解回调的工作原理,请使用以下链接:

This StackOverflow question

答案 1 :(得分:1)

me.checkNo(function(){ return checkNo;})