Re: xul, javascript and manipulating svg

Neil <[email protected]> Thu, 03 Sep 2009 00:45:48 +0100
Newsgroups gmane.comp.mozilla.devel.svg
Message-ID <9aidnQNA6L4wngLXnZ2dnUVZ_q6dnZ2d__812.361381440674$1251935453$gmane$org@mozilla.org>
Tony Cox wrote:

>I could call global functions from the listeners and get those to call functions on the renderer - but that seems ugly.
>  
>
You can use addEventListener to add inner functions or event objects to 
your elements.
If you use inner functions then when they are called back they will have 
access to variables declared in their parent function.
If you use event objects then when their handleEvent method is called 
the "this" keyword will refer to the object.
Some trivial examples:
function addAlertClickFunction(element, message) {
    function alertClickFunction(event) {
       alert(message); // accessing variable from parent function
    }
    element.addEventLIstener("click", alertClickFunction, false);
}
function addAlertClickObject(element, message) {
    var alertClickObject = {
       message: message, // copies message from parent function
       handleEvent: function(event) {
          alert(this.message); // accesses copy of variable
       }
    };
    element.addEventListener("click", alertClickObject, false);
}
Which style you use tends to depend on the style of your code; if it is 
already heavily object-oriented then the object style often works better 
(in some cases you can make the parent object an event listener) but if 
it is function-oriented then the function style usually works better.

-- 
Warning: May contain traces of nuts.