5

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?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Roberto Schuster
  • 293
  • 2
  • 5
  • 16

5 Answers5

6

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);

RoboKozo
  • 4,981
  • 5
  • 51
  • 79
3

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.

Jonathan
  • 51
  • 3
1

Try this:

Ext.getCmp('My-Button').menu.refOwner
oxyacanthous
  • 88
  • 2
  • 8
1
var MyButton = MyItemMenu3.up('button');

or for ExtJS3:

var MyButton = MyItemMenu3.findParentByType('button');
Molecular Man
  • 22,277
  • 3
  • 72
  • 89
0

In the latest version of ExtJS at least (4.2), every Ext.menu.Item has a parentMenu property that can access the parent menu. You might subclass this for custom menu items.

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139