Zk 从选定选项卡中删除事件侦听器 [英] Zk Remove event Listener from selected tab

查看:46
本文介绍了Zk 从选定选项卡中删除事件侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个选项卡,一个是父选项卡,另一个是子选项卡.我创建了父选项卡并使用 onClose 事件动态地将侦听器放置到它,然后创建子选项卡.实际上,我希望当用户单击 Tab 的关闭按钮时,他无法关闭并获取消息,因此我将 event.stopPropogation() 用于处理关闭事件.创建子选项卡后,应从父选项卡中删除事件侦听器.但侦听器不会从父选项卡中删除.因为我正在使用 removeEventlistener 但它也不起作用.

I have 2 tabs one is Parent tab and other is child tabs.I have created Parent tab and put listener using onClose event dynamically to it and then creating child tabs. Actually I want that when user clicks on close button of Tab, he is not able to close and get message so I put event.stopPropogation() to handle the close event. After creation of child tabs, event listener should be removed out from parent tab.But listener is not removing from parent tab. As I am using removeEventlistener but it is also not working.

我第一次调用一个方法,在该方法中我将事件侦听器添加到父选项卡.

First Time I am calling a method in which I am adding event listener to the parent tab.

 mainTab.getSelectedTab().addEventListener(Events.ON_CLOSE,
                new EventListener<Event>() {
                    public void onEvent(Event event) throws Exception {

    event.stopPropogation();
        showWarning(message);
    return;

}
                });

然后在创建所有子标签后我必须删除这个监听器.我正在使用...

then after creating all child Tabs I have to remove this listener.I am using...

mainTab.getSelectedTab().removeEventListener(Events.ON_CLOSE,
                    new EventListener<Event>() {
                        public void onEvent(Event event) throws Exception {



    }
                    });

此侦听器不在此选项卡上工作,但是当我打开新选项卡(主选项卡的同级)时,不会调用侦听器.

this listener is not working on this tab but when I open new tab (sibling of main tab) then listener is not called.

如何从当前选项卡中删除侦听器?

How to remove listener from current tab?

谁能解决我的问题?

推荐答案

感谢示例,这很有帮助.这里的问题在于 removeEventListener 函数的使用.该函数的第二个参数,一个 EventListener 实例,实际上是将被移除的 exact 事件监听器.您可以在 ZK 源代码中看到这一点;removeEventListener 函数在 AbstractComponent在第 2140 行,它检查一个已知的 EventListener,它等于函数参数.

Thanks for the example, that helped a lot. The problem here was in the use of the removeEventListener function. The second argument of the function, an EventListener instance, is actually the exact event listener that will be removed. You can see this in the ZK source code; the removeEventListener function is implemented in AbstractComponent and on line 2140 it checks for a known EventListener which equals the function argument.

这是一个有效的修复:

public class Controller extends SelectorComposer<Window> {

 private static final EventListener<Event> EVENT_STOPPER = new EventListener<Event>() {
   public void onEvent(Event event) throws Exception {
        event.stopPropagation();
        System.out.println("Stopped propagation of " + event);
   }
 };

 @Wire
 private Tabbox mainTab;

 @Override
 public void doAfterCompose(Window comp) throws Exception {
   super.doAfterCompose(comp);
   addCloseEventStopper();
 }

 @Listen(Events.ON_CLICK + " = #addTabsButton")
 public void addTabsButtonClicked() {
   removeCloseEventStopper();
   addTabs();
 }

 private void addCloseEventStopper() {
   mainTab.getSelectedTab().addEventListener(Events.ON_CLOSE, EVENT_STOPPER);
 }

 private void removeCloseEventStopper() {
   mainTab.getSelectedTab().removeEventListener(Events.ON_CLOSE, EVENT_STOPPER);
 }

 private void addTabs() {
   Tabs tabs = mainTab.getTabs();
   tabs.appendChild(new Tab("Tab Two"));
   tabs.appendChild(new Tab("Tab Three"));
 }

}

这里的关键是 addEventListenerremoveEventListener 中使用了相同的 EventListener 实例.

The key thing here is that the same EventListener instance is used in addEventListener and removeEventListener.

注意这里我们使用了一个 private static final 内部类,这只是保持对一个引用的一种方式事件监听器.根据您的用例,还有其他方法.

Note here we've used a private static final inner class, this is just one way to hold on to a reference to an EventListener. There are other ways depending on your use case.

这篇关于Zk 从选定选项卡中删除事件侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆