[ 3 posts ]

Scott Green

  • Username: spgre
  • Joined: Tue Jul 14, 2009 3:36 pm
  • Posts: 2
  • IRC: spgre
  • Offline
  • Profile

Excute Call Back Funtion returned by Event.getListener()

Post Posted: Tue Jul 14, 2009 3:52 pm
+0-
Hi all,

I am having trouble with executing the call back function for an attached event. I use the getListeners() function to return the click event listener that I have attached to the control via Event.addListener(). When the results of the getListeners() call is dumped into the FireBug console it shows the fn property as having the function assigned to it, however when I try to execute it I get the error clickListener.fn is not a function.

I have posted a example of what I am trying to achieve below. I am using FF3 and Chrome. Any one got any ideas as to why this happens?

Thanks in advance..

Code:
<html>
  <head>
    <script type="text/javascript" src="http://yui.yahooapis.com/combo?2.7.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
    <script>
      var namespace = function() {
        YAHOO.util.Event.addListener(window, "load", Init);
       
        function Init() {
          YAHOO.util.Event.addListener("cmdClickMe", "click", cmdClickMe_Click);
          YAHOO.util.Event.addListener("cmdClickOther", "click", cmdClickOther_Click);         
        }
       
        function cmdClickMe_Click(oArgs) {
          alert("You clicked me!");
        }
       
        function cmdClickOther_Click(oArgs) {
          var clickListener = YAHOO.util.Event.getListeners("cmdClickMe", "click");
          clickListener.fn(); // throws the error: clickListener.fn is not a function
        }
      }();
    </script>
  </head>
  <body>
    <input type="button" id="cmdClickMe" value="Click Me" />
    <input type="button" id="cmdClickOther" value="Click 'Click Me'" />
  </body>
</html>

Adam Moore

YUI Contributor

  • Username: adam
  • Joined: Wed Sep 03, 2008 11:16 am
  • Posts: 356
  • GitHub: apm
  • Gists: apm
  • Offline
  • Profile
Tags:

Re: Excute Call Back Funtion returned by Event.getListener()

Post Posted: Wed Jul 15, 2009 11:28 am
+0-
getListeners returns an array:
Code:
        function cmdClickOther_Click(oArgs) {
          var clickListeners = YAHOO.util.Event.getListeners("cmdClickMe", "click");
          clickListeners[0].fn();
        }

Scott Green

  • Username: spgre
  • Joined: Tue Jul 14, 2009 3:36 pm
  • Posts: 2
  • IRC: spgre
  • Offline
  • Profile
Tags:

Re: Excute Call Back Funtion returned by Event.getListener()

Post Posted: Wed Jul 15, 2009 2:11 pm
+0-
Thanks Adam,

Worked a treat. Something so simple has had me pulling my hair out for quite some time.
  [ 3 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum