使用vanilla GWT的匿名内部类中的奇怪行为

时间:2015-05-17 15:30:26

标签: gwt

public class TestComposite extends Composite{
    private String str = "hello";

    public TestComposite() {
        Keymaster.get().bindShortcuts("alt+up", new Keymaster.KeymasterHandler() {
            @Override
            public void process(String shortcut) {
                if (isAttached()) {
                    GWT.log("do the job");//why enter here even if TextComposite is detached
                }
            }
        });
    }
}

上面的类是一个GWT组合,让我感到困惑的是,为什么即使TextComposite被分离也会运行该行?(见代码)

KeyMaster是我的mousetrap.js包装器,它的键盘绑定是全局的,这意味着即使TestComposite被分离,当我按下快捷方式时仍然会执行process()方法。所以我使用isAttached()来防止它在DOM中没有显示时运行,但是DID运行且isAttached()返回true,有人可以解释一下吗?

1 个答案:

答案 0 :(得分:0)

isAttached!= isVisible。 isAttached的javadoc说

'Determines whether this widget is currently attached to the browser's 
 document (i.e., there is an unbroken chain of widgets between this widget 
 and the underlying browser document).'

尝试

if(isVisible()) {
  ...
}