How do you attach click events to ExtJS template elements?

空扰寡人 提交于 2019-12-22 05:59:08

问题


How would I add a click event to each link tag in this other than by building in onclick=.... into the XTemplate?

new Ext.XTemplate(
    '<ul>',
    '<tpl for="."><li><a href="#{anchor}">{text}</a></li></tpl>',
    '</ul>'
).overwrite('someElement', [
    { text: 'Click me', anchor: '1' },
    { text: 'No, click me', anchor: '2'}
]);

回答1:


The short answer is, you don't. Instead, you should use event delegation:

Ext.get('someElement').on('click', function(event, target) {
    console.log(target);
}, null, {delegate: 'a'});

This has 2 main advantages:

  1. You only need to bind a single listener
  2. It will work as you dynamically modify the content


来源:https://stackoverflow.com/questions/8794148/how-do-you-attach-click-events-to-extjs-template-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!