PDA

View Full Version : Event handling for menu in extjs 4



rvini2006
12 Aug 2011, 3:49 AM
Hi

We are using Extjs 4 for our application development. We are trying to implement dynamic controller loading. (http://www.sencha.com/forum/showthread.php?132795-Dynamically-loading-MVC-controller) For this to work we need to have the menu's selection events captured in our controller. In our current design we are using the 'handler' to capture the menu click event. But we would want to change the design to have a view where we define our menu details, the selection event menu items need to be captured with in the controller. Any tips/ suggestion will of great help. Thanks in advance.

Currently our implementation is
Ext.define('App.view.layout.AppMenu', {
extend : 'Ext.menu.Menu',
alias : 'widget.AppMenu',
layout : 'fit',
plain : true,
floating : false, // usually you want this set to True (default)
renderTo : Ext.getBody(), // usually rendered by it's containing component
items : [ {
text : 'Users',
handler : onItemClick,
iconCls : 'user_info_icon',
}, {
text : 'Managers'

}

});


function onItemClick() {
// steps based on the menu items selected...
}

slemmon
12 Aug 2011, 5:18 AM
So you have a controller for your app that could listen for menu's events now, correct?
If so, you should be able to add a listener for the menuItem's click event and place your logic in a function there.
http://docs.sencha.com/ext-js/4-0/#/api/Ext.app.Controller-method-control


E (http://docs.sencha.com/ext-js/4-0/#/api/Ext.app.Controller-method-control)xt.define('App.controller.MainAppController', {
init: function () {
this.control ({
'AppMenu > menuitem': {
click: this.onAppMenuClick
}
})
}
, onAppMenuClick: function (menuitem, e, opt) {
// steps based on the menu items selected...
}
})

rvini2006
12 Aug 2011, 8:22 AM
Appreciate your quick response! I was able to capture the click event but I'm not able to get the selected item text. I get menuitem as undefined in "onAppMenuClick" function. Not sure if I'm missing something here.

jay@moduscreate.com
12 Aug 2011, 8:38 AM
You should listen to the menu's click event, not the item:

click( Ext.menu.Menu menu, Ext.Component item, Ext.EventObject e, Object options )

rvini2006
12 Aug 2011, 11:11 AM
I added the following code. Now I'm able to handle the click event and also I'm able to get the selected menu text.



init : function(application) {

this.control({
'AppMenu > menuitem' : {
click : this.onItemClick
}
});
},

onItemClick:function(menuitem){
switch (menuitem.text) {
// logic is added here
}
}


Thanks all for the inputs

gnube
12 Sep 2011, 4:39 AM
thanks for answers here - saved me too

The selector can also include the menu item id - maybe split out actions without the switch
eg. 'menu > menuitem#refresh'

thx