Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > howto install event handler

Reply
Thread Tools

howto install event handler

 
 
nstasiv@gmail.com
Guest
Posts: n/a
 
      08-31-2006
Hello,
the idea is to make object draggable with code like this

var callout = new Callout(...);
new Draggable(callout);
where new Callout(...) adds some elements to DOM tree and new Draggale
does the trick:

function Draggable(_obj){
this.obj = _obj;
this.clickedX=0;
this.clickedY=0;
this.origX=0;
this.origY=0;
this.obj.onmousedown = function(evt){
this.pickup(evt);
}
this.obj.onmouseup = function(evt){
this.dropoff(evt);
}
this.obj.onmousemove = function(evt){
this.givealift(evt);
}
}

Draggable.prototype.givealift = function(evt)...
Draggable.prototype.pickup = function(evt)...
Draggable.prototype.dropdown = function(evt)...

I used to install event handler with e.setAttribute("onmousedown",
"pickup(evt)")
and everything worked fine, then I made scripts in object oriented way
but code
this.obj.onmousedown = function(evt){
this.pickup(evt);
}
won't work as expected, please help!

Thank you
Nazar

 
Reply With Quote
 
 
 
 
Richard Cornford
Guest
Posts: n/a
 
      08-31-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
<snip>
> this.obj.onmousedown = function(evt){
> this.pickup(evt);
> }
> won't work as expected, please help!


In javascript the value of - this - is determined entirely by how a
function is called and has no relationship with how or where code that
uses it is declared/defined. When the browser calls an event handler
assigned to an event handling property of an Element it calls that
handler as a method of the Element, and so - this - is a reference to
the Element.

There are several techniques for associating DOM Element's event
handlers with javascript object instances. Many are based upon
closures:-

<URL: http://jibbering.com/faq/faq_notes/closures.html >

Richard.

 
Reply With Quote
 
 
 
 
nstasiv@gmail.com
Guest
Posts: n/a
 
      08-31-2006
As I understand code

this.ctl.getObject().addEventListener("mousedown",
function(evt){this.pickup(evt);}, false);

has the problem where this.pickup(evt); is called. Am I right?

If there is no easy way to do the trick I will dig into closures
subject. Thank you for help!

Richard Cornford wrote:
> (E-Mail Removed) wrote:
> <snip>
> > this.obj.onmousedown = function(evt){
> > this.pickup(evt);
> > }
> > won't work as expected, please help!

>
> In javascript the value of - this - is determined entirely by how a
> function is called and has no relationship with how or where code that
> uses it is declared/defined. When the browser calls an event handler
> assigned to an event handling property of an Element it calls that
> handler as a method of the Element, and so - this - is a reference to
> the Element.
>
> There are several techniques for associating DOM Element's event
> handlers with javascript object instances. Many are based upon
> closures:-
>
> <URL: http://jibbering.com/faq/faq_notes/closures.html >
>
> Richard.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Event Handler that creates adds another event handler kaczmar2@gmail.com ASP .Net 1 02-22-2007 07:37 AM
problem in changing the actual event handler into our own event ashok.dhananjeyan@gmail.com Javascript 0 10-30-2006 02:32 PM
rendering Button inside Render() event, makes it loose its click event handler association sonic ASP .Net 1 01-07-2005 06:33 PM
How to recall add event from an Event handler?? RC ASP .Net Web Controls 1 01-06-2005 07:44 PM
Passing event from dynamically created element and event handler? Adi Javascript 2 02-23-2004 02:44 PM



Advertisments