Brandon
Brandon

Reputation: 2084

What's the best way to add an Ext JS button click listener after render?

I'm trying to figure out the best way to add a Button click listener later, after render.

For example if I have this code, how would I add a click listener later?

Ext.application({
    name : 'Fiddle',

    launch : function() {
        var button = Ext.create('Ext.Button', {
            text: 'Button'
        });
        Ext.Viewport.add({ xtype: 'container', padding: 10, items: [button] });


        // TODO How can I add a click listener later?
        setTimeout(function() {

        }, 1000);
    }
});

Answered Below Example

Based on the answer below here's a quick code snippet. https://fiddle.sencha.com/#view/editor&fiddle/309g

Ext.application({
    name : 'Fiddle',

    launch : function() {
        var button = Ext.create('Ext.Button', {
            text: 'Button'
        });
        Ext.Viewport.add({ xtype: 'container', padding: 10, items: [button] });

        button.on("tap", function () {
            alert("tap 1 works");
        });

        button.addListener("tap", function() {
            alert("tap 2 works");
        });

        button.el.on("click", function() {
           alert("click works");
        });
    }
});

Upvotes: 0

Views: 1241

Answers (1)

norbeq
norbeq

Reputation: 3076

You need to use method addListener (or on shorthand method).

button.on("tap", function () {
    alert("clicked");
});

Check in fiddle https://fiddle.sencha.com/#view/editor&fiddle/309e

Upvotes: 1

Related Questions