1. #1
    Sencha User
    Join Date
    Aug 2011
    Posts
    3
    Vote Rating
    0
    rvini2006 is on a distinguished road

      0  

    Default 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...
    }

  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...
        }
    })

  3. #2
    Sencha - Support Team slemmon's Avatar
    Join Date
    Mar 2009
    Location
    Boise, ID
    Posts
    2,320
    Vote Rating
    64
    Answers
    170
    slemmon is just really nice slemmon is just really nice slemmon is just really nice slemmon is just really nice slemmon is just really nice

      0  

    Default


    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...
        }
    })

  4. #3
    Sencha User
    Join Date
    Aug 2011
    Posts
    3
    Vote Rating
    0
    rvini2006 is on a distinguished road

      0  

    Default


    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.

  5. #4
    Sencha - Community Support Team jay@moduscreate.com's Avatar
    Join Date
    Mar 2007
    Location
    Frederick MD, NYC, DC
    Posts
    16,169
    Vote Rating
    28
    Answers
    83
    jay@moduscreate.com is a jewel in the rough jay@moduscreate.com is a jewel in the rough jay@moduscreate.com is a jewel in the rough jay@moduscreate.com is a jewel in the rough

      0  

    Default


    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.

  6. #5
    Sencha User
    Join Date
    Aug 2011
    Posts
    3
    Vote Rating
    0
    rvini2006 is on a distinguished road

      0  

    Default


    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.

    Code:
    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

  7. #6
    Sencha User
    Join Date
    May 2011
    Posts
    70
    Vote Rating
    0
    Answers
    4
    gnube is on a distinguished road

      0  

    Default


    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

Tags for this Thread