A method called twice GWT

I am building a web application with GWT. I have a bookMode () method that creates a user interface with some text fields, lists, etc. And shows it in a dialog box. I call the method as follows:

//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();
                }   
            }
            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 is not called twice. I have no idea why I like GWT so much. The userMode () method is also called twice.

+3
source share
5 answers

Java HTML Javascript; java javascript . , () HTML , , . , , .

+5

, - ClickHandler .

- :

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

" !" , .

MVP. , Presenter , Place. , Presenter , click, ClickHandler .

0

:

1) ,

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

arg0, / .. , .

2)   → private RegistrationHandler stopHandler;

, click/mouse/keypress, :

    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();<-----------------
    }
});

stopHandler.removeHandler(), .

p.s: removeHandler(), stopHandler(.dot)

0

GWT.

"Widgets.java", . "ClassName.ButtonName", : "Widgets.myButton".

, , RootPanel, , , KEPT ​​ NEW ONE.

, , .

? "" .

"" , , .:)

0

Here's how I solved it: I declared a handler registration variable this way

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 simply call stopPropagation (), preventDefault () and delete the handler to make sure that it does not fire more than once

0
source

Source: https://habr.com/ru/post/1793285/


All Articles