Loading ...
Sorry, an error occurred while loading the content.

subscribe vs. addListener?

Expand Messages
  • Derek
    What s the difference or relationship between subscribe() and addListener()?
    Message 1 of 4 , Jul 2, 2008
    • 0 Attachment
      What's the difference or relationship between subscribe() and addListener()?
    • Satyam
      You use subscribe to listen to a custom event, one produced by the component (YAHOO.util.CustomEvent
      Message 2 of 4 , Jul 2, 2008
      • 0 Attachment
        You use subscribe to listen to a custom event, one produced by the
        component (YAHOO.util.CustomEvent
        <http://developer.yahoo.com/yui/docs/YAHOO.util.CustomEvent.html>). You
        use addListener or on to listen to a DOM event (YAHOO.util.Event
        <http://developer.yahoo.com/yui/docs/YAHOO.util.Event.html>).

        Some YUI components listen to DOM events and fire their own custom
        event. Element (YAHOO.util.Element
        <http://developer.yahoo.com/yui/docs/YAHOO.util.Element.html>) is one
        such component (and all those that inherit from it) so you might get to
        subscribe to a custom event that initially was triggered by the DOM. In
        Element and all those components that inherit from it, addListener, on
        and subscribe are synonyms.

        Satyam


        Derek wrote:
        > What's the difference or relationship between subscribe() and addListener()?
        >
        >
        > ------------------------------------
        >
        > Yahoo! Groups Links
        >
        >
        >
        > ------------------------------------------------------------------------
        >
        >
        > No virus found in this incoming message.
        > Checked by AVG.
        > Version: 8.0.101 / Virus Database: 270.4.3/1529 - Release Date: 01/07/2008 19:23
        >
      • Mingfai
        hi Satyam, I have a related question/comment. When I use different YUI components, the way to subscribe/listen to events seems inconsisent. The following is a
        Message 3 of 4 , Jul 2, 2008
        • 0 Attachment
          hi Satyam,

          I have a related question/comment. When I use different YUI components, the way to subscribe/listen to events seems inconsisent. The following is a quick summary by looking up the doc and my code:
          • Element (extends EventProvider)
            subscribe(), on/addListener()

          • Layout, LayoutUnit, Resize, Editor (extends Element)
            subscribe(), on/addListener(), xxxEvent.subscribe()

          • DataTable (extends EventProvider)
            subscribe()

          • Container (not extending EventProvider/Element)
            subscribe()

          • DD (not extending EventProvider/Element)
            YAHOO.util.Event.on(xxx,'click' ), on() [but not the same as Element.on, and no addListener?] , onXXX

          • HTML Element
            YAHOO.util.Event.on( id, 'click')

          The above are probably incomplete. And my purpose is just to try to find out what's the rule behind. It seems:
          • There are diff ways to subscribe/listen: subscribe, on, addListener, xxxEvent.subscribe, YAHOO.util.Event.on(), and individual onXXXX methods.

          If I want to learn/memorize the subscription/listener mechanism, what are the rules? And what's the recommended way? Am I correct to say:
          1. First check if a component events Element or Event Provider, if so, then on/subscribe() will be available. The branch of components that extends Element are actually quite clear.

          2. If any component with event (as in doc), we can always use instanceName.eventName.subscribe, or YAHOO.util.Event.on( id , eventName, ..)  to subscribe to it.

          3. For other case, lookup the doc case by case. For HTMLElement events, it's always YAHOO.util.Event.on()

          Any comment? Thank you very much.

          Regards,
          mingfai





          On Wed, Jul 2, 2008 at 6:07 PM, Satyam <satyam@...> wrote:

          You use subscribe to listen to a custom event, one produced by the
          component (YAHOO.util.CustomEvent
          <http://developer.yahoo.com/yui/docs/YAHOO.util.CustomEvent.html>). You
          use addListener or on to listen to a DOM event (YAHOO.util.Event
          <http://developer.yahoo.com/yui/docs/YAHOO.util.Event.html>).

          Some YUI components listen to DOM events and fire their own custom
          event. Element (YAHOO.util.Element
          <http://developer.yahoo.com/yui/docs/YAHOO.util.Element.html>) is one
          such component (and all those that inherit from it) so you might get to
          subscribe to a custom event that initially was triggered by the DOM. In
          Element and all those components that inherit from it, addListener, on
          and subscribe are synonyms.

          Satyam



          Derek wrote:
          > What's the difference or relationship between subscribe() and addListener()?
          >
          >
          > ------------------------------------
          >
          > Yahoo! Groups Links
          >
          >
          >
          > ----------------------------------------------------------
          >
          >
          > No virus found in this incoming message.
          > Checked by AVG.
          > Version: 8.0.101 / Virus Database: 270.4.3/1529 - Release Date: 01/07/2008 19:23
          >

        • Satyam
          There is no rule behind any of this. The components of the library have been developed over a long time. Element is quite new and has brought some
          Message 4 of 4 , Jul 2, 2008
          • 0 Attachment
            There is no rule behind any of this. The components of the library have
            been developed over a long time. Element is quite new and has brought
            some consistency to those components developed with it and even some
            that were developed earlier and have been upgraded to extend Element.
            Other components could not be upgraded in that way due to
            incompatibilities that would rise if attempted. Thus, inconsistencies
            remain and not only in the handling of events. You 'render' most of the
            components but you 'draw' the TreeView and the DataTable renders
            automatically on instantiation.

            The rules you list below look good enough, though I guess that they
            could be simplified: simply use on, that's a safe bet.

            Don't despair, though, version 3 will bring consistency on all this,
            though at the cost of some compatibility issues. There comes a time
            when you cannot sacrifice everything for backward compatibility.

            Satyam


            Mingfai wrote:
            > hi Satyam,
            >
            > I have a related question/comment. When I use different YUI
            > components, the way to subscribe/listen to events seems inconsisent.
            > The following is a quick summary by looking up the doc and my code:
            >
            > * Element (extends EventProvider)
            > subscribe(), on/addListener()
            >
            > * Layout, LayoutUnit, Resize, Editor (extends Element)
            > subscribe(), on/addListener(), xxxEvent.subscribe()
            >
            > * DataTable (extends EventProvider)
            > subscribe()
            >
            > * Container (not extending EventProvider/Element)
            > subscribe()
            >
            > * DD (not extending EventProvider/Element)
            > YAHOO.util.Event.on(xxx,'click' ), on() [but not the same as
            > Element.on, and no addListener?] , onXXX
            >
            > * HTML Element
            > YAHOO.util.Event.on( id, 'click')
            >
            > The above are probably incomplete. And my purpose is just to try to
            > find out what's the rule behind. It seems:
            >
            > * There are diff ways to subscribe/listen: subscribe, on,
            > addListener, xxxEvent.subscribe, YAHOO.util.Event.on(), and
            > individual onXXXX methods.
            >
            >
            > If I want to learn/memorize the subscription/listener mechanism, what
            > are the rules? And what's the recommended way? Am I correct to say:
            >
            > 1. First check if a component events Element or Event Provider, if
            > so, then on/subscribe() will be available. The branch of
            > components that extends Element are actually quite clear.
            >
            > 2. If any component with event (as in doc), we can always use
            > instanceName.eventName.subscribe, or YAHOO.util.Event.on( id ,
            > eventName, ..) to subscribe to it.
            >
            > 3. For other case, lookup the doc case by case. For HTMLElement
            > events, it's always YAHOO.util.Event.on()
            >
            >
            > Any comment? Thank you very much.
            >
            > Regards,
            > mingfai
            >
            >
            >
            >
            >
            > On Wed, Jul 2, 2008 at 6:07 PM, Satyam <satyam@...
            > <mailto:satyam@...>> wrote:
            >
            > You use subscribe to listen to a custom event, one produced by the
            > component (YAHOO.util.CustomEvent
            > <http://developer.yahoo.com/yui/docs/YAHOO.util.CustomEvent.html>).
            > You
            > use addListener or on to listen to a DOM event (YAHOO.util.Event
            > <http://developer.yahoo.com/yui/docs/YAHOO.util.Event.html>).
            >
            > Some YUI components listen to DOM events and fire their own custom
            > event. Element (YAHOO.util.Element
            > <http://developer.yahoo.com/yui/docs/YAHOO.util.Element.html>) is one
            > such component (and all those that inherit from it) so you might
            > get to
            > subscribe to a custom event that initially was triggered by the
            > DOM. In
            > Element and all those components that inherit from it,
            > addListener, on
            > and subscribe are synonyms.
            >
            > Satyam
            >
            >
            >
            > Derek wrote:
            > > What's the difference or relationship between subscribe() and
            > addListener()?
            > >
            > >
            > > ------------------------------------
            > >
            > > Yahoo! Groups Links
            > >
            > >
            > >
            > > ----------------------------------------------------------
            > >
            > >
            > > No virus found in this incoming message.
            > > Checked by AVG.
            > > Version: 8.0.101 / Virus Database: 270.4.3/1529 - Release Date:
            > 01/07/2008 19:23
            > >
            >
            >
            >
            > ------------------------------------------------------------------------
            >
            >
            > No virus found in this incoming message.
            > Checked by AVG.
            > Version: 8.0.101 / Virus Database: 270.4.3/1529 - Release Date: 01/07/2008 19:23
            >
          Your message has been successfully submitted and would be delivered to recipients shortly.