Nuri Engin
Nuri Engin

Reputation: 823

Javascript: Why can not get variable in callback function?

I've read several different callback function documentation but unfortunately couldn't be success to get related variable. What I'm missing on here?

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

UPDATE: After Rahul Khandelwal's answer re-factored the functions and of course now works.

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

Upvotes: 0

Views: 241

Answers (2)

Rahul Khandelwal
Rahul Khandelwal

Reputation: 71

While calling the function you are not using callback functionality. Change it to the normal function definition like below.

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.

}

To understand how the callback works, please use below link:

This StackOverflow question

Upvotes: 1

John Willson
John Willson

Reputation: 455

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

Upvotes: 1

Related Questions