views:

806

answers:

3

I've read and followed YUI's tutorial for subscribing to Menu events. I also looked through the API and bits of the code for Menu, MenuBar, and Custom Events, but the following refuses to work

// oMenuBar is a MenuBar instance with submenus
var buyMenu = oMenuBar.getSubmenus()[1];

// this works
buyMenu.subscribe('show', onShow, {foo: 'bar'}, false);

// using the subscribe method doesn't work
buyMenu.subscribe('mouseOver', onMouseOver, {foo: 'bar'}, false);

// manually attaching a listener doesn't work
YAHOO.util.Event.addListener(buyMenu, 'mouseOver', onMouseOver);

// http://developer.yahoo.com/yui/docs/YAHOO.widget.Menu.html#event_keyPressEvent        
// there is a keyPress Event, but no spelling of it will trigger the handler
buyMenu.subscribe('keypress', onShow, {foo: 'bar'}, false);
buyMenu.subscribe('keypressed', onShow, {foo: 'bar'}, false);
buyMenu.subscribe('keyPressed', onShow, {foo: 'bar'}, false);
buyMenu.subscribe('keyPress', onShow, {foo: 'bar'}, false);

Functionally, I'm trying to attach a keyPress listener for each submenu of the MenuBar. I do not want to add Bubbling library as a dependency.

A: 

Based on my testing, the following will work:

oMenu.subscribe('keypress', function () { alert("I'm your friendly neighborhood keypress listener.")});

but that only fires when the Menu is receiving the keypress event, so it would need to already have focus.

Hank Gay
A: 

Does onShow point to a function?

eg.

var onShow = function()
{
    alert("Click!");
}
Adam Peck
+2  A: 

Hello Mark -

Todd Kloots here, author of the YUI Menu widget. When you are subscribing to DOM-based events, the event name is all lower case. So, for the "mouseover" event, subscribe as follows:

buyMenu.subscribe('mouseover', onMouseOver, {foo: 'bar'}, false);

Regarding your keypress event handler: you are subscribing correctly. However, remember that any key-related event handlers will only fire if the Menu has focus. So, make sure your Menu has focus before testing your key-related event handlers. Also - I would recommend listening for the "keydown" event rather than "keypress" as not all keys result in the firing of the "keypress" event in IE.

If you have any other questions, please direct them to the ydn-javascript Y! Group as I monitor the messages on that group frequently.

I hope that helps.

  • Todd