adding a new function to an event handler...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ian Richardson

    #1

    adding a new function to an event handler...

    I'm writing some code which does one thing when onreadystatecha nge
    occurs, e.g.

    handle.onreadys tatechange = function() { blah(handle,oth er_params) };

    ....but sometimes I need to add another, so it becomes:

    handle.onreadys tatechange = function() {
    whatever_was_pr eviously_define d(whatever);som e_other_functio n(handle) };

    How can I easily add some_other_func tion(handle) without knowing
    whatever_was_pr eviously_define d(whatever)?

    Note it's important that the functions are executed in this order.

    Thanks,

    Ian
  • RobB

    #2
    Re: adding a new function to an event handler...

    Ian Richardson wrote:[color=blue]
    > I'm writing some code which does one thing when onreadystatecha nge
    > occurs, e.g.
    >
    > handle.onreadys tatechange = function() { blah(handle,oth er_params) };
    >
    > ...but sometimes I need to add another, so it becomes:
    >
    > handle.onreadys tatechange = function() {
    > whatever_was_pr eviously_define d(whatever);som e_other_functio n(handle)[/color]
    };[color=blue]
    >
    > How can I easily add some_other_func tion(handle) without knowing
    > whatever_was_pr eviously_define d(whatever)?
    >
    > Note it's important that the functions are executed in this order.
    >
    > Thanks,
    >
    > Ian[/color]

    var oldhandler = handle.onreadys tatechange;
    handle.onreadys tatechange = function()
    {
    if (null != oldhandler)
    {
    oldhandler();
    }
    some_other_func tio­n(handle);
    }

    Comment

    • Michael Winter

      #3
      Re: adding a new function to an event handler...

      RobB wrote:

      [snip]
      [color=blue]
      > var oldhandler = handle.onreadys tatechange;
      > handle.onreadys tatechange = function()
      > {
      > if (null != oldhandler)
      > {
      > oldhandler();
      > }
      > some_other_func tio­n(handle);
      > }[/color]

      That would be fine as long as oldhandler doesn't use the this operator
      to refer to handle. In such cases, either make oldhandler a property
      of handle, or change the existing call to

      oldhandler.call (this);

      The former is probably preferable due to Microsoft's late
      implementation of the call method.

      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • RobB

        #4
        Re: adding a new function to an event handler...

        Michael Winter wrote:[color=blue]
        > RobB wrote:
        >
        > [snip]
        >[color=green]
        > > var oldhandler = handle.onreadys tatechange;
        > > handle.onreadys tatechange = function()
        > > {
        > > if (null != oldhandler)
        > > {
        > > oldhandler();
        > > }
        > > some_other_func tio­n(handle);
        > > }[/color]
        >
        > That would be fine as long as oldhandler doesn't use the this[/color]
        operator[color=blue]
        > to refer to handle. In such cases, either make oldhandler a property
        > of handle, or change the existing call to
        >
        > oldhandler.call (this);
        >
        > The former is probably preferable due to Microsoft's late
        > implementation of the call method.
        >
        > Mike[/color]

        Mike: thanks.

        Like this?

        var oldhandler = handle.onreadys tatechange;
        handle.onreadys tatechange = function()
        {
        if (null != oldhandler)
        {
        handle.oldhandl er = oldhandler;
        handle.oldhandl er();
        }
        some_other_func tio­n(handle);
        }

        Comment

        • Michael Winter

          #5
          Re: adding a new function to an event handler...

          RobB wrote:

          [snip]
          [color=blue]
          > Mike: thanks.[/color]

          You're welcome.
          [color=blue]
          > Like this?[/color]

          [snip]

          Yes, though you may as well avoid the global.

          handle.oldHandl er = handle.onreadys tatechange;
          handle.onreadys tatechange = function() {
          if(handle.oldHa ndler) {handle.oldHand ler();}
          someOtherFuncti on(this);
          };

          The added benefit of this approach is that allows this to be applied
          to as many objects as necessary.

          [snip]

          Mike

          --
          Michael Winter
          Replace ".invalid" with ".uk" to reply by e-mail.

          Comment

          • Grant Wagner

            #6
            Re: adding a new function to an event handler...

            "Ian Richardson" <zathras@chaos. org.uk> wrote in message
            news:hR0Sd.1506 $LR3.1003@newsf e5-gui.ntli.net...[color=blue]
            > I'm writing some code which does one thing when onreadystatecha nge
            > occurs, e.g.
            >
            > handle.onreadys tatechange = function() { blah(handle,oth er_params) };
            >
            > ...but sometimes I need to add another, so it becomes:
            >
            > handle.onreadys tatechange = function() {
            > whatever_was_pr eviously_define d(whatever);som e_other_functio n(handle)
            > };
            >
            > How can I easily add some_other_func tion(handle) without knowing
            > whatever_was_pr eviously_define d(whatever)?
            >
            > Note it's important that the functions are executed in this order.[/color]

            The already provided suggestions are all workable, but I'd like to throw
            my solution into the ring:

            function addDelegate(ele ment, elementEvent, delegate)
            {
            // multi-cast delegates are attached to the element as an Array;
            // ie - element['onclickDelegat es'], element['onfocusDelegat es'],
            etc
            var elementDelegate s = elementEvent + 'Delegates';

            if (element[elementDelegate s])
            {
            // there are already multi-cast delegates for this element
            // and this event; add this delegate
            element[elementDelegate s].push(delegate) ;
            }
            else
            {
            // no multi-cast delegates for this element for this event;
            // create a multi-cast delegate list for the element
            element[elementDelegate s] = [];

            if (element[elementEvent])
            {
            // this element has a delegate for this event already;
            // preserve the delegate and append it to the list of
            // multi-cast delegates
            element[elementEvent + 'Delegate'] = element[elementEvent];
            element[elementDelegate s].push(element[elementEvent]);
            }

            // append the new delegate to the list of multi-cast delegates
            element[elementDelegate s].push(delegate) ;

            // change the event to a function which will fire all
            // multi-cast delegates
            element[elementEvent] = (function() {
            return function()
            {
            for (var ii = 0; ii < this[elementDelegate s].length;
            ++ii)
            {
            this[elementDelegate s][ii].call(this);
            }
            }
            })();
            }
            } // addDelegate()

            function removeDelegate( element, elementEvent, delegate)
            {
            // multi-cast delegates are attached to the element as an Array;
            // ie - element['onclickDelegat es'], element['onfocusDelegat es'],
            etc
            var elementDelegate s = elementEvent + 'Delegates';

            if (element[elementDelegate s])
            {
            // multi-cast delegates for this element were found

            var delegatesRemove d = 0;

            if (elementEvent == delegate)
            {
            // SPECIAL CASE: when the delegate to be removed is the same
            // String as the event, attempt to remove the preserved
            // original delegate for this element for this event;
            // this is done because the original delegate may be an
            // anonymous function built from the original HTML;
            // ie - <input ... onclick="...">
            delegate = element[elementEvent + 'Delegate'];
            }

            // iterate through the multi-cast delegates; when a matching
            // one is found, remove it from the list
            var ii = 0;
            while (ii < element[elementDelegate s].length)
            {
            if (element[elementDelegate s][ii] == delegate)
            {
            element[elementDelegate s].splice(ii, 1);
            ++delegatesRemo ved;
            }
            else
            {
            ++ii;
            }
            }

            // return the number of multi-cast delegates removed
            return delegatesRemove d;
            }

            // no multi-cast delegates were found for this element; return
            // something to give the caller this information
            return -1;
            } // removeDelegate( )

            The above functions allow you to add as many delegates (event handlers)
            as you want to an event. Note that I have only ever used it with form
            elements, I'm assuming it will work with host objects.

            In your case, you'd use:

            addDelegate(han dle, 'onreadystatech ange', function() { ... });

            Note that if you declare the delegate anonymously as shown above, you
            will be unable to remove it later. To avoid this problem, declare the
            delegate concretely:

            function myDelegate() {
            removeDelegate( this, 'onreadystatech ange', myDelegate);
            // do stuff
            }
            addDelegate(han dle, 'onreadystatech ange', myDelegate);

            By the way, I've also never tested the above, it just occurred to me
            that you should be able to remove a delegate from an object from within
            the delegate.

            My solution suffers from the fact that it relies on a method not added
            until JScript 5.6, so it may not be suitable for the general use
            Internet.

            --
            Grant Wagner <gwagner@agrico reunited.com>
            comp.lang.javas cript FAQ - http://jibbering.com/faq


            Comment

            Working...