问题描述
我正在尝试在 Ext JS 4 中创建我自己的 LinkButton 组件.没什么新东西,对吧?
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);
}
}
});
到目前为止一切顺利!我的 LinkButton 看起来确实像一个超链接,我的文本内容就在其中.优雅.
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 链接按钮组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!