At 2002-07-11 19:15, James Musick wrote:
> So if e represents the event/element triggering the event and you create multiple 
>event listeners, how are they all kept straight?
>
> For example:
>alert(this.name);[result=foo]
>var l = new EventListener(this);
>l.oncreate = function(e)
>{
> var o = e.getTarget();
> alert(o.name);[result=foo]
> ....more code...
>
> Both alerts in the above snippet give 'foo' as a response.
> An event listener was created, linking it to 'this'.
> Then you used the oncreate function of the EventListener to execute the function. 
>But 'e' is passed into the function.
> If this is an 'element' to which an eventListener is linked, which event listener is 
>passing it's associated element in? Because 'foo' returns both times it seems like 
>e.getTarget() must be returning this (which is indeed named 'foo'). l is the only 
>EventListener associated with 'this' at this time in the code!
>
> Hmm. Any ideas/clarifications? I keep getting turned around when thinking about the 
>fact that e is always handy and there's never confusion when creating multiple 
>EventListeners about what e is.
> Maybe e is a member variable/object of the current DynLayer and by passing e into 
>the EventListener functions you're ensuring that if something happens to an event 
>handler (could be any one of multiple event handlers) the 'root' DynLayer that you 
>want to affect is indeed changed?
>
> Well, thanks for the help/input. It seems silly to get bogged down in this , but I 
>like to know what the arguments being passed back and  forth are, and with DynAPI 
>there are a lot of implicit objects associated with being a DynLayer that I see used, 
>but not explained.
>
> Cheers,
> James

While I read and re-read what you wrote I interpreted it differently each time, so if 
the following is missing your question just tell me.

When you write code such as this (your example above):

        var l = new EventListener(this);
        l.oncreate = function(e)
        {
         var o = e.getTarget();
         alert(o.name);[result=foo]
         ....more code...

the following happens:

Line 1:
1. You create a new object.
2. The object is of type "EventLister".
3. The "EventListener" constructor gets the current value of "this" as an argument.
4. A reference to the created object is stored in variable "l".

Line 2:
You are making an anonymous function (there is no name between "function" and "(e)") 
that takes one argument, "e", that can be used within that anonymous function, and you 
are storing the reference to the function as the "oncreate" property of "l" (and by 
doing so it can be used as a method on that object).
What this means is that "e" isn't evaluated at that time, but rather when it is called.


When you call a method, for example

        obj.methodname(arg);

javascript will make the special variable "this" be the same as "obj", calls the 
function "methodname" of that object with the argument(s) supplied. When the method 
call returns it restores what the "this" variable was before (you can call a method 
that calls a method of another object and so on).

In listeners.js there is this code:

        EventListener.prototype.handleEvent=function(type,e,args) {
                if(this["on"+type]) this["on"+type](e,args);
        };

and later in that file

        DynObject.prototype.invokeEvent=function(type,e,args) {
                if (!e) e=new DynEvent(type,this);
                if (this.hasEventListeners) for(var 
i=0;i<this.eventListeners.length;i++) {
                        e.target=this.eventListeners[i].target;
                        this.eventListeners[i].handleEvent(type,e,args);
                }
                if(e.bubble && this.parent) {
                        e.preBubbleCode();
                        e.src = this.parent;
                        this.parent.invokeEvent(type,e,args);
                }
        }

(The argument "type" is a string that says what type of event it is, for example 
"create".)

The "e" I think you are asking about is created by the first line in the "invokeEvent" 
method (if it wasn't passed in). If there are event listeners the "handleEvent" method 
is called which calls the method named what "type" is with "on" as a prefix 
("oncreate" in this example). For each method call the "this" variable points to 
different objects.

So, how far off am I to what you wanted to know?

/Lunna



-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
PC Mods, Computing goodies, cases & more
http://thinkgeek.com/sf
_______________________________________________
Dynapi-Help mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dynapi-help

Reply via email to