Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the parent of a menu?

I'm trying to get the component in which a menu is linked. Take a look:

    Ext.create('Ext.Button', {
    id: 'MyButton',
    text: 'Click me',
    renderTo: Ext.getBody(),
    menuAlign: 'tl-bl',
    menu: {
        itemId: 'MyMenu',        
        forceLayout: true,
        items:
        [
            {
                text  : 'Option 1',
                itemId: 'MyItemMenu1'
            }, {
                text  : 'Option 2',
                itemId: 'MyItemMenu2'
            }, {
                text   : 'Get the parent!',
                itemId : 'MyItemMenu3',
                handler: function(){
                    
                    // Get the item menu.
                    var MyItemMenu3 = this; 
                    alert(MyItemMenu3.getItemId()); 
                    
                    // Get the menu.
                    var MyMenu = MyItemMenu3.ownerCt; 
                    alert(MyMenu.getItemId());
                    
                    // Try to get the button.
                    var MyButton = MyMenu.ownerCt; 
                    alert(MyButton);                    
                    
                    // Returns:                    
                    // 'MyItemMenu3'
                    // 'MyMenu'
                    // undefined                 
                }
            }
        ]
    }
});

Online example: http://jsfiddle.net/RobertoSchuster/mGLVF/

Any idea?

like image 576
Roberto Schuster Avatar asked Aug 01 '11 21:08

Roberto Schuster


3 Answers

I'm learning EXT myself so I'm not too sure what's going on but I think I was able to get it this way: console.log(MyMenu.floatParent.id);

like image 173
RoboKozo Avatar answered Nov 19 '22 00:11

RoboKozo


In our ExtJS 4 project, we eventually just patched the up() function on AbstractComponent

Ext.AbstractComponent.override({
    up: function(selector) {
        var result = this.ownerCt||this.floatParent;
        if (selector) {
            for (; result; result = result.ownerCt||result.floatParent) {
                if (Ext.ComponentQuery.is(result, selector)) {
                    return result;
                }
            }
        }
        return result;
    }
});

This makes up() walk up the ownerCt chain, but if ownerCt isn't defined, it looks to floatParent.

Now you can call cmp.up('some-selector'); even if its a menu or menu item.

like image 27
Jonathan Avatar answered Nov 19 '22 00:11

Jonathan


Try this:

Ext.getCmp('My-Button').menu.refOwner
like image 1
oxyacanthous Avatar answered Nov 19 '22 01:11

oxyacanthous