-
12 Aug 2011 3:49 AM #1
Answered: Event handling for menu in extjs 4
Answered: Event handling for menu in extjs 4
Hi
We are using Extjs 4 for our application development. We are trying to implement dynamic controller loading. (http://www.sencha.com/forum/showthre...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...
}
-
Best Answer Posted by slemmon
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
Code:E
Code: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... } })
-
12 Aug 2011 5:18 AM #2
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
Code:E
Code: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... } })
-
12 Aug 2011 8:22 AM #3
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.
-
12 Aug 2011 8:38 AM #4Sencha - Community Support Team
- Join Date
- Mar 2007
- Location
- Frederick MD, NYC, DC
- Posts
- 16,169
- Vote Rating
- 28
- Answers
- 83
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 )
Jay Garcia @ModusJesus || Modus Create co-founder
Ext JS in Action author
Sencha Touch in Action author
Get in touch for Ext JS & Sencha Touch Touch Training
We are also working on Video-based Sencha Touch training: Check it out here.
-
12 Aug 2011 11:11 AM #5
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.
Thanks all for the inputsCode:init : function(application) { this.control({ 'AppMenu > menuitem' : { click : this.onItemClick } }); }, onItemClick:function(menuitem){ switch (menuitem.text) { // logic is added here } }
-
12 Sep 2011 4:39 AM #6
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


Reply With Quote