ExtJS4 LinkButton component component

I'm trying to create my own LinkButton component in Ext JS 4. Nothing new, right?

My code looks like this:

Ext.define('LinkButton', {
    extend: 'Ext.Component',
    xtype: 'linkbutton',
    autoEl: 'a',
    renderTpl: '<a href=\"javascript:;\">{text}</a>',
    config: {
        text: '',
        handler: function () { }
    },
    initComponent: function () {
        var me = this;
        me.callParent(arguments);

        this.renderData = {
            text: this.getText()
        };

        var handler = me.getHandler();
        if (handler) {
            me.on('click', handler);
        }
    }
});

      

So far so good! My LinkButton really looks like a hyperlink and my text content is there. Graceful.

However, I cannot get my component to fire an event when I click on it!

This particular line me.on('click', handler);

does n't work ! Even if I change it from on to addListener it has no effect.

So the question is, how do I add DOM events to my component? Or better yet, how do I access the DOM element of my own component? I couldn't do anything!

Thank!

+3


source to share


3 answers


Here is my suggestion based on the source from the component Button

:

Ext.define('LinkButton', {
    extend: 'Ext.Component',
    xtype: 'linkbutton',
    autoEl: 'a',
    renderTpl: '<a href=\"javascript:;\" id="{id}-btnEl">{text}</a>',
    config: {
        text: '',
        handler: function () { }
    },
    initComponent: function () {
        var me = this;
        me.callParent(arguments);

        this.renderData = {
            text: this.getText()
        };
    },
    onRender: function(ct, position) {
        var me = this, 
            btn;

        me.addChildEls('btnEl');

        me.callParent(arguments);

        btn = me.btnEl;

        me.mon(btn, 'click', me.onClick, me);
    },
    onClick: function(e) {
        var me = this;
        if (me.preventDefault || (me.disabled && me.getHref()) && e) {
            e.preventDefault();
        }
        if (e.button !== 0) {
            return;
        }
        if (!me.disabled) {
            me.fireHandler(e);
        }
    },
    fireHandler: function(e){
        var me = this,
            handler = me.handler;

        me.fireEvent('click', me, e);
        if (handler) {
            handler.call(me.scope || me, me, e);
        }
    }
});

      



Working example: http://jsfiddle.net/lolo/AEwH4/1/

+2


source


Or you can do this



afterRender : function() {
    this.callParent(arguments);

    this.mon(this.el, {
        scope : this,
        delegate : 'a',
        click : this.handleClick
    });
},

handleClick : function(e, t) {
    e.stopEvent();

    var handler = this.getHandler();
    if (handler) {
        handler();
    }
}

      

+3


source


the right way is to use Ext.view.View for such tasks. There is a custom html that allows you to do anything and at the same time all the functionality of Ext.Component is available.

0


source







All Articles