Hi!
My problem is that in callback functions (onApeAddUser, onMyCommand),
the variable 'this' is not a reference to the instance of MyClass
(namely, 'myInstance'), but rather on the [object global].


1/ Is it the expected behavior for APE (If so, why ?) or a javascript
'standard behavior' (not the same as Java or C++ for example) ?
Yes it is (for JS). In Fact it depends on how and with which scope 
the appropriate callback functions are called. If they were called like 
this: myInstance.onApeAddUser.call(myInstance, params, 
infos);or myInstance.onApeAddUser.apply(myInstance, [params, infos]);they´d be 
scoped to the myInstance object and this in the functionswould point to it 
(myInstance). 
2/ Is there any convenient way to make 'this' points to the instance
holding the callback function ?
Or by default, any way to be able to pass the callback a reference to
the instance of MyClass ?Well first there is to say that what you use is the 
Mootools/Prototype way ofcreating pseudo classes. There are actually no things 
like classes in JSsince it´s an object based language (not oriented). So there 
are no classesbut only objects. (functions are also objects) So I show you one 
(there are more) way to solve your problem. To understandscoping etc. In 
JavaScript there are (for example) a lot of good talks on 
YUiTheatre:http://developer.yahoo.com/yui/theater/Especially the ones of 
Douglas Crockford are the ones to look for ("Advanced JavaScript").
A function in JS is actually a function and the "this" in it points to the 
global object(in browsers this is the "window"  object). Now when you call a 
function with the "new" operator it becomes some kind of a constructor an 
"this" points  to the resulting object:
var myObject = function (){   this.foo = 'bar';}
var myInst = new  myObject();myInst.foo == 'bar' // true
Now you can introduce a 'method' for the object
var myObject = function(){   this.foo = 'bar';   this.func = function(){      
return this.foo;   }}
var myInst = new  myObject();myInst.func() == 'bar' // true
This works, but however it still depends on how the method is called.(You could 
still call it with a different scope)To be absolutely sure to point to the 
myInst object, you can do the following(which indeed looks a little bit odd):
var myObject = function(){   this.foo = 'bar';   var me = this;   // this is 
the object we return   return {      func : function {         return me.foo    
   }   }}
var myInst = new  myObject();myInst.func() == 'bar' // allways true
Actually you could put in this example foo in a scopedvariable like var foo = 
'bar' to call it directly, but it´s justfor the example. Also this is the way 
to implement "private" properties ormethods in JS.
Hope this helps a little.(If you want to know how to do this in 
Mootools/Prototype style,please, look in their documentations)
Timo
--- nouknouk <[email protected]> schrieb am Do, 11.3.2010:

Von: nouknouk <[email protected]>
Betreff: [APE Project] [JS server side] 'this' in Ape events and commands  
callback functions.
An: "APE Project" <[email protected]>
Datum: Donnerstag, 11. März, 2010 16:00 Uhr

Hi,

I wanted to refactor some of my code in a nice Javascript 'class'.
The class has to register itself during initialization for several APE
events and commands

Sample class code:

var MyClass = new Class({
    Implements: [Options],

    options: {
        blablabla: 0
    },

    initialize: function(options) {
        this.setOptions(options);
        Ape.addEvent('adduser', this.onApeAddUser);
        Ape.registerCmd("my_command", true, this.onMyCommand);
    },

    onApeAddUser: function(params, infos) { Ape.log("onApeAddUser;
"+this.options.blablabla); },
    onMyCommand: function(params, infos) { Ape.log("onMyCommand:
"+this.options.blablabla); }
}

An instance of MyClass is created in 'global' scope like that:
var myInstance = new MyClass, { blablabla: 27 });

My problem is that in callback functions (onApeAddUser, onMyCommand),
the variable 'this' is not a reference to the instance of MyClass
(namely, 'myInstance'), but rather on the [object global].


1/ Is it the expected behavior for APE (If so, why ?) or a javascript
'standard behavior' (not the same as Java or C++ for example) ?

2/ Is there any convenient way to make 'this' points to the instance
holding the callback function ?
Or by default, any way to be able to pass the callback a reference to
the instance of MyClass ?

3/ I also noticed the same kind of behavior for several classes which
always return a reference on the Ape object that fired the event, not
the holder of the callback function (like callbacks of 'Ape.Mysql' and
'Ape.sockClient').


Thanks in advance.

Nouk²

-- 
You received this message because you are subscribed to the Google
Groups "APE Project" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/ape-project?hl=en
---
APE Project (Ajax Push Engine)
Official website : http://www.ape-project.org/
Git Hub : http://github.com/APE-Project/


__________________________________________________
Do You Yahoo!?
Sie sind Spam leid? Yahoo! Mail verfügt über einen herausragenden Schutz gegen 
Massenmails. 
http://mail.yahoo.com 

-- 
You received this message because you are subscribed to the Google
Groups "APE Project" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/ape-project?hl=en
---
APE Project (Ajax Push Engine)
Official website : http://www.ape-project.org/
Git Hub : http://github.com/APE-Project/

Reply via email to