开发者

Method called twice GWT

I'm creating a web application with GWT. I have a method bookMode() that builds a UI with some textboxes, listboxes, etc. and shows it in a dialog box. I call the method like this:

//listen for mouse events on the add button
    addButton.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {

            String check = Cookies.getCookie("role");
            if(check == "TopBeheerder") {   
                boolean currentMode = mode0.getValue();
                add addObject = new add();
                if(currentMode == true) {
                    addObject.bookMode();
                }
                if(currentMode == false) {
                    addObject.userMode();
   开发者_C百科             }   
            }
            else {
                BibPhp.notification("You don't have enough permissions.");
            }


        }
    });

When I use the GWT.log() function I notice that this method is called twice. But I searched all my code with the eclipse search function and the method isn't called twice. I have no idea why GWT behaves likes this. The method userMode() is also called twice.


When the Java code gets converted to HTML and Javascript; the equality comparison between java and javascript are not the same. Hence if you have two widgets (buttons) with the same id in HTML and add click handlers to each one by one, chances are high that the same widget gets the click handler added twice. Try setting different ids to the button widgets, that has helped me to resolve any issues related multiple click handler invocations.


There are two ways to fix this problem :

1) put after the object that fires twice this two methods

Event.stopPropagation();
Event.preventDefault();

or just replace Event with arg0 if you are using key/mouse etc. handlers. If this doesn't stop from calling it twice go with the second way

2) declare in the class this variable -> private RegistrationHandler stopHandler;

then when you add the click/mouse/keypress handler write this :

    stopHandler=addButton.addClickHandler(new ClickHandler() {
    public void onClick(ClickEvent event) {

        String check = Cookies.getCookie("role");
        if(check == "TopBeheerder") {   
            boolean currentMode = mode0.getValue();
            add addObject = new add();
            if(currentMode == true) {
                addObject.bookMode();
            }
            if(currentMode == false) {
                addObject.userMode();
            }   
        }
        else {
            BibPhp.notification("You don't have enough permissions.");
        }

      stopHandler.removeHandler();<-----------------
    }
});

put stopHandler.removeHandler() where do you need to stop from repeating twice.

p.s: if you don't find removeHandler() method, write stopHandler(.dot) and then search for the method


My guess is that, somehow, you are adding the ClickHandler twice.

I would do something like this to debug:

System.out.println("adding click handler!");
addButton.addClickHandler(new ClickHandler() { ...

If you see "adding click handler!" print twice, that is your answer.

I ran into a similar problem a while back when implementing an MVP pattern. The View was static and initialized only once, however, the Presenter was initialized each time the Place was requested. Each time the Presenter was initialized it would bind the click event, in turn adding the ClickHandler multiple times.


I had a similar problem with GWT Buttons.

I was creating a Button inside a Class called "Widgets.java", without writing the listener in that class. Then I was using this button as "ClassName.ButtonName", like a macro: "Widgets.myButton".

Of course, at a certain point, I was adding a listener to this referenced button from another class and although I was deleting and cleaning the full RootPanel, when I had to redraw again the button with the same name and so, it seemed that the button KEPT the listener already associated and added A NEW ONE again.

So every time I clicked that button, the function associated to it, ran twice.

Solution? Just stop using a "reference" to a Button and create it locally.

I read this thread and that "enlightened" me to reach my solution, just wanted to make my two cents. :)


Here's how I solved it : I declared an Handler Registration variable like this

private HandlerRegistration HandlerButton;

then when you need to call the addbutton method,use the variable created before

HandlerButton=addButton.addClickHandler(new ClickHandler() {
    public void onClick(ClickEvent event) {

        String check = Cookies.getCookie("role");
        if(check == "TopBeheerder") {   
            boolean currentMode = mode0.getValue();
            add addObject = new add();
            if(currentMode == true) {
                addObject.bookMode();
            }
            if(currentMode == false) {
                addObject.userMode();
            }   
        }
        else {
            BibPhp.notification("You don't have enough permissions.");
        }

      event.stopPropagation();
      event.preventDefault();     
      HandlerButton.removeHandler();
    }
});

at the end of the method you just call stopPropagation(),preventDefault() and remove the handler to be sure that it doesn't fires more than one time

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜