Search code examples
javascriptextjsextjs6extjs6-classic

Running a controller function from an ExtJS 6.6 href link


Is it possible to call a controller function from a href tag inside a html string? ... I can't seem to get anything working so i think i may be doing something wrong.

Im using ExtJS 6.6

I have some user information and then a logout link, but nothing works, this is what i have.

items: [{
    region: 'north',
    height: 200,
    items: [{
        xtype: 'panel',
        layout: 'column',
        items: [{
            xtype: 'panel',
            columnWidth: 0.3
        }, {
            xtype: 'panel',
            columnWidth: 0.7,
            html: '<div class="userstuff" style="text-align: right;">Hello, welcome to my page<br /><a href="#" class="logout">Log out</a></p></div>'
        }]
    }]
}]

The bit that i cant get going is;

<a href="#" class="logout">Log out</a>

If i use a button xtype, i can call it through the handler, and it works perfect, but i now need to change it to a text link.

This is the button code that works;

{
    xtype: 'button',
    text: 'Logout',
    handler: 'onLogout'
}

Any help getting this text link to call onLogout would be fantastic.

Thanks


Solution

  • You could call onLogout using Event delegation.You need to attach listener on your panel like this

    {
        xtype: 'panel',
        listeners: {
            element: 'el',
            delegate: 'a.logout',
            click: 'onLogoutClick'
        }
    }
    

    You can check here with working fiddle.

    Code Snippet

    Ext.define('MyViewController', {
        extend: 'Ext.app.ViewController',
        alias: 'controller.myview',
    
        /**
         * This event will fire on Logout click
         */
        onLogoutClick: function(e) {
            console.log(e);
            Ext.Msg.alert('Success', 'You have clicked on logout button');
        }
    });
    
    Ext.create({
        xtype: 'panel',
        title: 'Running a controller function from an ExtJS 6.6 href link',
        layout: 'border',
        controller: 'myview',
        height: 200,
        items: [{
            region: 'north',
            height: 200,
            items: [{
                xtype: 'panel',
                layout: 'column',
                items: [{
                    xtype: 'panel',
                    columnWidth: 0.3
                }, {
                    xtype: 'panel',
                    columnWidth: 0.7,
                    listeners: {
                        element: 'el',
                        delegate: 'a.logout',
                        click: 'onLogoutClick'
                    },
                    html: '<div class="userstuff" style="text-align: right;">Hello, welcome to my page<br /><a href="#" class="logout">Log out</a></p></div>'
                }]
            }]
        }],
    
        renderTo: Ext.getBody()
    });