www.webdeveloper.com
Results 1 to 2 of 2

Thread: How to access calling object in OnSuccess/OnFailure?

  1. #1
    Join Date
    Jan 2010
    Posts
    5

    How to access calling object in OnSuccess/OnFailure?

    Hi guys,
    I'm making this ajax call:

    Code:
    new Ajax.Request(url, {
    		   method: 'get',
    		   onSuccess: function(transport) {
    		       this.contextReady = true;
    		       
    		   },
    	   	   onFailure: function(transport) {
    		        this.contextReady = false;
    		   }
    			   
    	   });
    from a JS object containig contextReady. The problem is that this within the OnSuccess is not the class but something else (e.g. a window).
    How can I update the right this.contextReady?

    Thanks for any hints!

  2. #2
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    Create a variable reference to the 'this' object:
    Code:
    var myObj = {
        contextReady : false,
        makeRequest : function (url) {
            var obj = this; //holder to reference myObj's 'this'
            new Ajax.Request(url, {
                method: 'get',
                onSuccess: function(transport) {
                    obj.contextReady = true;
                },
                onFailure: function(transport) {
                    obj.contextReady = false;
                }     
            });
        }
    };

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles