[ 6 posts ]

Nate Cavanaugh

YUI Contributor

  • Offline
  • Profile

addListener, subscribe, on, and event names

Post Posted: Wed Feb 18, 2009 1:22 pm
+0-
Another random question that I can't seem to get my head around and it's probably just me, but I'm having a hard time figuring out the differences between subscribing to different events.

From what I can gather, on is sometimes an alias to addListener and sometimes an alias to subscribe. Is this true, or am I imagining that?

Also, the rule I can decipher out is:
addListener: for DOM events
subscribe for custom events

Is that also true?

Lastly, this is about event names and consistency. A good example is the following:

Code:
var panel = new YAHOO.widget.Panel(YAHOO.util.Dom.generateId(), {draggable: true});

panel.setHeader('myTest')
panel.render(document.body);

panel.subscribe('drag', function(){
console.log('drag', this, arguments);
});

panel.dd.subscribe('startDragEvent', function(){
console.log('startDrag', this, arguments);
});


I think the confusing part is that in the documentation, the event listed for the panel is dragEvent, but doesn't work when you subscribe to that event name.

So is this just a random inconsistency, or is there a method to the madness I'm not seeing, and would be helpful to know?

Thanks in advance!
Nate Cavanaugh

Dav Glass

  • Username: davglass
  • Joined: Thu Aug 28, 2008 9:28 am
  • Posts: 2088
  • Location: Marion, IL, US
  • Twitter: davglass
  • GitHub: davglass
  • Gists: davglass
  • IRC: davglass
  • Offline
  • Profile
Tags:

Re: addListener, subscribe, on, and event names

Post Posted: Wed Feb 18, 2009 1:25 pm
+0-
Nate --

There is a reason, but it's not a real good one ;)

The reason for the naming issues is that some of the code was written early on in the YUI 2.x timeline and they support an older way of subscribing to events.

The newer classes (as well as 3.x) all have a common naming scheme:
Code:
instance.on('something', function() {})


Does that make sense?

Nate Cavanaugh

YUI Contributor

  • Offline
  • Profile
Tags:

Re: addListener, subscribe, on, and event names

Post Posted: Wed Feb 18, 2009 1:57 pm
+0-
Dav,
Sounds good, now I know to look out for it.

Since the docs are slightly misleading (since the event listed is different from the actual event to subscribe to), is there any quick way to know, or is it just testing it out?

I think my worry is that as I'm developing, I'll have to write the event signature a couple different ways, even with the docs just to see, which can get maddening, ya know?

Thanks again :)
Nate Cavanaugh

Dav Glass

  • Username: davglass
  • Joined: Thu Aug 28, 2008 9:28 am
  • Posts: 2088
  • Location: Marion, IL, US
  • Twitter: davglass
  • GitHub: davglass
  • Gists: davglass
  • IRC: davglass
  • Offline
  • Profile
Tags:

Re: addListener, subscribe, on, and event names

Post Posted: Wed Feb 18, 2009 1:58 pm
+0-
If there is an event that isn't documented properly, please file a bug for it:
http://yuilibrary.com/projects/yui2/newticket

That's the best way to get it fixed..

Nate Cavanaugh

YUI Contributor

  • Offline
  • Profile

Re: addListener, subscribe, on, and event names

Post Posted: Wed Feb 18, 2009 2:27 pm
+0-
That I will do, however, I'm noticing something here: would it be safe to assume that if it's listed as a custom event, it's always a property on that object?

For instance, when it says dragEvent as the custom event of DragDrop, does that mean every instance has a dragEvent property like so:

var dragItem = new YAHOO.util.DD(el, options);

dragItem.dragEvent.subscribe

?

If so, it could simply be a bug in my brains programming and not the documentation :)

For some reason, I always believed the custom events to be keys to subscribe to, not properties on that object.

If I'm wrong on this one, too, I'll file a ticket :D
Nate Cavanaugh

Dav Glass

  • Username: davglass
  • Joined: Thu Aug 28, 2008 9:28 am
  • Posts: 2088
  • Location: Marion, IL, US
  • Twitter: davglass
  • GitHub: davglass
  • Gists: davglass
  • IRC: davglass
  • Offline
  • Profile

Re: addListener, subscribe, on, and event names

Post Posted: Wed Feb 18, 2009 2:32 pm
+0-
Nate --

There are 2 main types of event subscriptions:

Events Example

Click here to see the revision history on this Gist.

They should be noted per component in either the API docs or their landing pages.
  [ 6 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