userx
userx

Reputation: 1121

How to pass the scope to the callback?

I have the following code structure and I am trying to maintain the scope of "this" to the current object in the success and error callbacks.

I have tried using Apply function, but I am not sure how to use in my use-case and that's where I need some help.

Here is the code I have :

function navigateToDocumentList(record){
  var self= this;
    var handoutDocumentList=Ext.create('xxx.view.HandoutsDocumentListPanel');
    var navigationView = self.getNavigationView();
    navigationView.push(handoutDocumentList);
    navigationView.getNavigationBar().show();
    navigationView.getNavigationBar().setTitle(record.data.name);
    var url =  intermountain.constants.HANDOUTS_DOCUMENTS_OF_CATEGORY_SERVICE_URL+record.data.name;
    self.makeAJAXCallWIthParams(url,
                               16000,
                               self.navigateToDocumentListSuccess,
                               self.navigateToDocumentListError,
                               self);
}


function makeAJAXCallWithParams(url, timeout, success, error, callbackObj) {
Ext.Viewport.setMasked({
                        xtype: 'loadmask',
                        message: 'Loading ...'
                    });
Ext.Ajax.request({
                url: url,
                timeout: 5000,
                success: success.apply(callbackObj,[]),// I need to know how to pass the function arguments here as earlier I was just calling it by success and now I need to call it using apply function
                failure: failure

            });
}

Please let me know if I need to explain the problem better. Any input / hint would be appreciated.

Thanks for your time!

Upvotes: 1

Views: 44

Answers (1)

hugomg
hugomg

Reputation: 69944

To answer your question about arguments, each function in Javasctript can access a list with all its arguments via the arguments pseudo-parameter:

success: function(){ return success.apply(callbackObj, arguments) }

However, its going to be simpler to use the bind method instead of apply or call.

success: success.bind(callbackObj),
failure: failure.bind(callbackObj)

Upvotes: 1

Related Questions