tags:

views:

41

answers:

5

I have this :

var MyObject = function(){   
   this.url = "monurl";   
   this.mavar = "";   
   this.Load = function(){
     var callback = {
       success: function(o){
         mavar = o.responseXML.getElementsByTagName("montag")[0].firstChild.nodeValue;
       }
     }
     YAHOO.util.Connect.asyncRequest('GET',url,callback);
} }

the mavar variable is not accessible. How can I do this ?

A: 

Save the this variable:

var MyObject = function() {   
   this.url = "monurl";   
   this.mavar = "";   
   this.Load = function() {
     var me = this;
     var callback = {
       success: function(o) {
         me.mavar = o.responseXML.getElementsByTagName("montag")[0].firstChild.nodeValue;
       }
     }
     YAHOO.util.Connect.asyncRequest('GET',url,callback);
  }
}
Max Shawabkeh
Also Great ! Thanks a lot.
The second approach (accessing it via the object name) isn't going to work. You're adding a property `mavar` to the constructor function object—not the actual `MyObject` object *instance*. The first way, saving `this` to another variable (I like to call it `_this` or `self`), is the best way to go.
Steve Harrison
@Steve: Right, edited. Didn't notice that it was a constructor rather than an object literal.
Max Shawabkeh
Right! I try it and my choice is to put the this to another variable.Thanks
A: 

You could also do it like this:

var MyObject = new function(){   
   this.url = "monurl";   
   this.mavar = "";   
   this.Load = 
   (function(that){ 
        return function(){
            var callback = {
                success: function(o){
                    that.mavar = o.responseXML.getElementsByTagName("montag")[0].firstChild.nodeValue;
                }
            }
            YAHOO.util.Connect.asyncRequest('GET',url,callback);
        } 
    })(this);
};
miensol
A: 

In my object, I add a variable that's reference the this :

var selfMyObject = this;

and I replace the this.mavar to selfMyObject.mavar in success callback and it's work fine.

A: 

The best way to handle this is to let YUI do your scope correction, since support is built in. Here's the docs page telling you how to to do it. http://developer.yahoo.com/yui/connection/#scope

var MyObject = function() {
    this.url = "monurl";
    this.mavar = "";
    this.Load = function() {
        var callback = {
            success: function(o){
                this.mavar = o.responseXML.getElementsByTagName("montag")[0].firstChild.nodeValue;
            },
            scope: this
        }

        YAHOO.util.Connect.asyncRequest('GET', url, callback);
    }
}
Tivac
ok thanks for your help. I try.
A: 

I believe you can set a scope param to the callback. So you should be able to do something like the following.

var MyObject = function(){   
   this.url = "monurl";   
   this.mavar = "";   
   this.Load = function(){
     var callback = {
       success: function(o){
         this.mavar = o.responseXML.getElementsByTagName("montag")[0].firstChild.nodeValue;
       },
       scope: this
     }
     YAHOO.util.Connect.asyncRequest('GET', this.url, callback);
} }
frglps