Help with the "this" pointer

Discussion in 'JavaScript' started by LJDesign, Apr 26, 2011.

  1. #1
    I am new to Java Script and I'm trying to wrap my brain around a problem I have. I have one JS function in a file that calls a JS function in a different file passing it some parameters. The second function does a database query and then calls a callback in the first function.

    The callback is then going to take the database data and update the UI with it. To get to the UI elements I need the "this" pointer in the first function. The problem is that when the callback gets called "this" no longer is the same "this" as before the database call. It is now a global "this" and not that function's "this". I know there is a way to solve this but I've seen different examples and none are exactly what I need so I still haven't figured it out.

    In my first function I make a call into the second function:

    this.dao.retrieveLogs(sqlParams, this.querySuccess, this.queryFailure);

    The second and third parameters point to local callback functions.

    In the second function the call is received:

    this.retrieveLogs = function (inQueryData, querySuccess, queryFailure)
    {
    try
    {
    this.db.transaction(
    (function (tx)
    {
    var sql = "SELECT * FROM 'log'";
    tx.executeSql(sql, [ ], this.executeHandler.bind(this, querySuccess), this.errorHandler.bind(this, queryFailure));
    }).bind(this)
    );
    }
    catch (e)
    {
    Mojo.Log.error(e, "Error running monthly query: " + sql);
    }
    },

    This is the the executreHandler() function in the same file that calls the callback:

    this.executeHandler = function(callback, transaction, results)
    {
    if (callback)
    callback(results);
    },

    Finally in the first function we have the callback:

    querySuccess: function(results)
    {
    // When I use this.something in here "this" is no longer the right "this"

    So how do I modify this to have the right "this" in my callback?
     
    LJDesign, Apr 26, 2011 IP
  2. Jan Novak

    Jan Novak Peon

    Messages:
    121
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    0
    #2
    I don't know if this is exactly your issue, but try to look at this code:

    
    function FBSession () {
        //this of FBSession 
        this.perms = null,
    
        this.getUserDataFromFB = function(type){
           var context=this;
           FB.getLoginStatus(function(response) {
              //here is different this, so I use context to fill this.perms for FBSession 
               if (response.session) {
                  context.perms = response.perms;
               }
            })
        }
    }
    
    Code (markup):
     
    Jan Novak, Apr 26, 2011 IP
  3. LJDesign

    LJDesign Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I really appreciate your answer Jan but I'm afraid I'm still lost. You may not want to but it would make a lot more sense to me if you modified my code above illustrating you this would work.
     
    LJDesign, Apr 26, 2011 IP
  4. LJDesign

    LJDesign Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ok I solved it and it was way easier than I thought. I simply needed to change my call to the database query from:
    this.dao.retrieveLogs(sqlParams, this.querySuccess, this.queryFailure);
    to this:
    this.dao.retrieveLogs(sqlParams, this.querySuccess.bind(this), this.queryFailure.bind(this));

    That restored the proper "this" when the callback was called.
     
    LJDesign, Apr 26, 2011 IP