www.webdeveloper.com
Results 1 to 2 of 2

Thread: Setting class variables from callback function

  1. #1
    Join Date
    Apr 2012
    Posts
    2

    Setting class variables from callback function

    Hi,
    So I'm building a class which runs function asynchronously. I want to set the class variables when the callback function returns the values. Unfortunetly I'm having some trouble doing so.
    This is my class:

    Code:
    function Users(){
            this.title = 'not defined';
    	this.url='user/';
    	};
    Users.prototype.load=function( scallbackf, ecallbackf,baseUrl) {
    		var v_usersload = new data_load(this.url,function(rObject){
    						this.title=rObject.title;
    						scallbackf();
    					},
    					function(errObj,err){
    						if (ecallbackf==undefined){
    						alert(err);
    						}else{
    						ecallbackf(errObj,err); 
    						}
    					},
    					baseUrl);
    };
    What i want is access to the classe's "title" variable so that the variables are set when the load method finishes executing. Unfortunetly, using this.title on the callback function defines the variable to the callback function instead of the class.
    Any ideas?
    Regards

  2. #2
    Join Date
    Apr 2012
    Posts
    2
    I found the answer. I just binded "this" to the callback function and presto!

    Code:
    function Users(){
            this.title = 'not defined';
    	this.url='user/';
    	};
    Users.prototype.load=function( scallbackf, ecallbackf,baseUrl) {
    		var v_usersload = new data_load(this.url,function(rObject){
    						this.title=rObject.title;
    						scallbackf();
    					}.bind(this),
    					function(errObj,err){
    						if (ecallbackf==undefined){
    						alert(err);
    						}else{
    						ecallbackf(errObj,err); 
    						}
    					},
    					baseUrl);
    };

Thread Information

Users Browsing this Thread

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

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