Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call parent object method inside function with callback handler

I'm calling ParentObject.doSomething() which inturn calls the WebService object to perform some ajax calls, and on success of the ajax call, the callback function is executed. But any parent function inside the callback function fails.

I think this has something to do with scope resolution. I am not able to find a workaround for this problem.

Is there a better architectural style to modularize the ajax services and model?

I've create a jsfiddle also - http://jsfiddle.net/bzKXr/2/

var ParentObject =  {

     doSomething: function(){
         document.write("Inside doSomething <br />");
         var self = this;
         WebServices.firstService("some URL", self.successCallback);
     },

     changeData: function(data){
         //Manipulate data
         document.write("Inside changeData <br />");
     },

     successCallback: function(jsonData){
         document.write("Inside successCallback <br />");
         try {
             //Execution fails at this point. Possibly because of scope resolution
             this.changeData(jsonData);  
         }
         catch (error) {
             console.log(error);
             document.write(error);
         }  
     },
};

var WebServices = {
    firstService: function(url, successCallbackHandler){
        document.write("Inside firstService <br />");
        //Get data using the URL
        //on success call successCallback
        successCallbackHandler("some data");
    }
};


$(document).ready(function() {
    ParentObject.doSomething();

});
​
like image 775
Anirudh S Avatar asked Jul 24 '12 12:07

Anirudh S


People also ask

Can I use an object method as a callback handler?

Another common expression of this problem is when an object method is used as a callback handler. Functions are prior in JavaScript, and the term “method” is considered a colloquial naming for a function, which is a value of an object property. That function doesn’t obtain a specific link to the containing object:

How to call parent class function from derived class function?

The following is an example to call parent class function from derived class function. The derived class d1 function is called. The parent class p1 function is called. In the above program, a parent class p1 is created and a function first () is defined in it. class p1 { public: void first () { cout << " The parent class p1 function is called.";

How to call a parent component method from the child component?

To call a parent component method from the child component, we need to pass the changeName () method as a prop to the child component and access it as a props data inside the child component. In the above code, we have passed the changeName () method as a prop to the Child component.

What is a callback interface in C?

C-style callback interfaces usually involve registering a function by providing: A void pointer to some private data (used internally by the callback function) C has no sense of objects, so passing in any function pointer will work just fine.


1 Answers

Writing self.successCallback doesn't bind the function to self, you have to actually call the function as self.successCallback() to bind the context correctly.

You can do that easily by wrapping the call in a closure which retains access to self:

doSomething: function(){
     document.write("Inside doSomething <br />");
     var self = this;
     WebServices.firstService("some URL", function() {
        self.successCallback();
     });
 },

or in ES5 you can use .bind() which ensures that the this context for successCallback is always the specified parameter:

doSomething: function(){
     document.write("Inside doSomething <br />");
     WebServices.firstService("some URL", this.successCallback.bind(this));
 },
like image 145
Alnitak Avatar answered Oct 17 '22 23:10

Alnitak