ExtJS4 链接按钮组件

ExtJS4 LinkButton Component(ExtJS4 链接按钮组件)
本文介绍了ExtJS4 链接按钮组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Ext JS 4 中创建我自己的 LinkBut​​ton 组件.没什么新东西,对吧?

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

我的代码如下所示:

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

到目前为止一切顺利!我的 LinkBut​​ton 看起来确实像一个超链接,我的文本内容就在其中.优雅.

So far so good! My LinkButton does look like a hyperlink anad my text content is in there. Graceful.

但是,当我单击组件时,我无法让组件触发事件!

However, I can't get my component to fire an event when I click on it!

这一行 me.on('click', handler); 工作!即使我将它从 on 更改为 addListener 也没有效果.

This particular line me.on('click', handler); is not working! Even if I change it from on to addListener it has no effect.

所以问题是:如何将 DOM 事件添加到我的组件中?或者,更好的是,我如何访问我自己的组件的 DOM 元素?我什么都做不了!

So question is: How do I add DOM events to my component? Or, even better, how do I access my own component's DOM element? I haven't been able to do any of that!

谢谢!

推荐答案

这是我的提议,它基于 Button 组件的源码:

Here is my proposition, which is basing on source from Button component:

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

工作示例:http://jsfiddle.net/lolo/AEwH4/1/

这篇关于ExtJS4 链接按钮组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Sencha Ext JS 4, trouble creating draggable panel with another panel(Sencha Ext JS 4,使用另一个面板创建可拖动面板时出现问题)
combobox should remember the last selection(组合框应该记住最后一次选择)
NextJS - Not Able To Use Custom Colors In Tailwind CSS In(NextJS-无法在TailWind中使用自定义颜色)
NextJS import images in MDX(NextJS在MDX中导入图像)
how to cleanly handle errors in nextjs getStaticProps(如何干净地处理nextjs getStaticProps中的错误)
Extjs 4 Grid hover effect(Extjs 4 网格悬停效果)