1

Forgive my inexperience but here's the problem, I have some 2 functions with 1 of them calling .getJSON and I need to call the 2nd function from within the first function's getJSON callback .. I am having a lot of trouble.

Here is a simplified version of what is going on.

MyDialog.Widget.prototype._myFunction1 = function(data) {
var self = this;

var renderEntry = function(tr,booltf) {
    $('<a href="javascript:{}">&nbsp;</a>')
            .addClass("iconbutton")
            .appendTo(tr.insertCell(0))
            .click(function() {
                $.getJSON(
                    "/command/button",
                    null,
                    function(data) {
                                      self.myFunction2(); //self isn't in scope?
                                      //this.myFunction2(); //this is actually 'this' for XHR request
                    },
                    "json"
                );
            });
//more code here (including the invoking of renderEntry()
}

MyDialog.Widget.prototype.myFunction2 = function()
{
 //Ton of code
}

The problem is in the callback function of the .getJSON , I want to call myFunction2 but I can't get it to call it... using 'this.myFunction2()' I am trying to access a 'myFunction2' inside the XHR request (that's what i see through firebug), and using the var 'self' that I create before doesn't work either because it isn't in scope I think, firebug doesn't show any variable 'self' when inside the callback function, and of course it just doesn't work when I try it out.

Any ideas? any more elaboration needed?

1
  • If self weren't in the scope you would get an error when trying to do self.something. Maybe your problem is something else? Commented May 1, 2011 at 18:18

2 Answers 2

5

Your code is fine in terms of scope. self is in scope and references this just like you want it to. Your debugger might not show it because it isn't in the local function scope, but that doesn't mean it won't be resolved correctly.

A simpler case can be easily demonstrated as follows:

var foo = {
   fn: function(){ console.log('Called!'); },
   invoker: function(){
      var self = this,
          local = function(){ setTimeout(function(){
                     setTimeout(function(){ self.fn(); }, 1000);
                  }, 1000); };
      local();
   }
};

foo.invoker();
  • Make sure your request is succeeding.
  • Remove the "json" argument ($.getJSON only has 3 parameters).
  • Post more code.
Sign up to request clarification or add additional context in comments.

Comments

-1

where are you calling the renderEntry() ? That's the key part of the code. It doesn't know self because it wasn't initialized when the renderEntry() function was called. The self is initialized when you call _myFunction1 and it goes dead when the function finishes execution.

You do realize that you are only initializing the renderEntry function and not invoking it?

1 Comment

No idea, what you mean by "goes dead", and I have strong suspicion that it isn't anything that is true of JS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.