howto install event handler

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nstasiv@gmail.com

    howto install event handler

    Hello,
    the idea is to make object draggable with code like this

    var callout = new Callout(...);
    new Draggable(callo ut);
    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.onmous edown = function(evt){
    this.pickup(evt );
    }
    this.obj.onmous eup = function(evt){
    this.dropoff(ev t);
    }
    this.obj.onmous emove = function(evt){
    this.givealift( evt);
    }
    }

    Draggable.proto type.givealift = function(evt).. .
    Draggable.proto type.pickup = function(evt).. .
    Draggable.proto type.dropdown = function(evt).. .

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

    Thank you
    Nazar

  • Richard Cornford

    #2
    Re: howto install event handler

    nstasiv@gmail.c om wrote:
    <snip>
    this.obj.onmous edown = 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.

    Comment

    • nstasiv@gmail.com

      #3
      Re: howto install event handler

      As I understand code

      this.ctl.getObj ect().addEventL istener("moused own",
      function(evt){t his.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:
      nstasiv@gmail.c om wrote:
      <snip>
      this.obj.onmous edown = 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.

      Comment

      Working...