[Proto-Scripty] Re: Mouse coordinates and event handlers

2009-01-31 Thread Sébastien Gruhier
You have to bind event handler to 'this'


 document.observe('mousemove', this.storeMousePosition.bind(this));
 $('testDiv').observe('mouseover', this.checkAndTell.bind(this));


Seb
---
http://xilinus.comWeb Application Development, Consulting, Training
http://mapeed.com Markers fusion plugin for your online maps


On Jan 31, 2009, at 10:37 PM, Stefano Esposito wrote:


 Hi all, can anyone tell me what's wrong with this piece of code
 (minimal example):

 === CODE ===

 html
 head
  script type=text/javascript src=js/prototype.js/script
  script type=text/javascript
   var testClass = Class.create({
initialize: function () {
 document.observe('mousemove', this.storeMousePosition);
 $('testDiv').observe('mouseover', this.checkAndTell);
},

storeMousePosition: function (event) {
 this.mouseX = event.pointerX();
 this.mouseY = event.pointerY();
},

checkAndTell: function () {
 $('ansDiv').update('x: '+this.mouseX+'; y: '+this.mouseY);
}
   });

   document.observe('dom:loaded', function () {
new testClass();
   });
  /script
 /head
 body
  div id=testDivThis is the mouseover div/div
  div id=ansDiv/div
 /body
 /html

 === END CODE ===

 It's not working in firefox 3.0.5 (gives x:undefined; y:undefined in
 div#ansDiv).

 The purpose is to store mouse coordinates in class
 variables as the mouse move and tell what's their value when the
 mouseover div#testDiv event happens.

 Thanks for your help :)

 Ciao,
 Stefano

 






--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Prototype  script.aculo.us group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~--~~~~--~~--~--~---



[Proto-Scripty] Re: Mouse coordinates and event handlers

2009-01-31 Thread Stefano Esposito

On Sat, 31 Jan 2009 22:47:24 +0100
Sébastien Gruhier sgruh...@gmail.com wrote:

 You have to bind event handler to 'this'
 
 
  document.observe('mousemove',
 this.storeMousePosition.bind(this));
 $('testDiv').observe('mouseover', this.checkAndTell.bind(this));
 
 
 Seb

This worked thanks :)

But now i've got a question. The documentation pdf says about the
observe method of the Event class, that Starting in Prototype 1.6,
Event handlers registered with observe are automatically bound to the
event’s target element in all browsers. This means that by default,
this in an event handler refers to the element that fired the event.

I'm using prototype 1.6.0.3, so if i'm not wrong, the call to
handler.bind(this) shouldn't be needed. Since it does not work
without that call, i'm obviously wrong. So: what the docs mean?

Stefano

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Prototype  script.aculo.us group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~--~~~~--~~--~--~---