简体   繁体   中英

GWT Mvp and handlers

I've been learning GWT the past couple months and found out that the Mvp is one of the best ways to design your project. I've read google's tutorial MVP part 1 and in their tutorial they put the clickHandlers ( for example) in the presenter. Now I had problems with that when constructing many view class that has many buttons with the same HTML id, and then the user interacts with these buttons... so if I have one button for every view, total 6 button. and the user clicks on one of them, the button will work 6 times for the same object... So I read and found out that it is better to put the handlers on the view class and create an event to the presenter.

So that what I did :

View Class :

         rb0.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {

                selectHandler.onEvent(1);
                System.out.print("rate 1");
            }
        });
         rb1.addClickHandler(new ClickHandler() {

                @Override
                public void onClick(ClickEvent event) {

                    selectHandler.onEvent(2);
                    System.out.print("rate 2");
                }
            });

         rb1.addClickHandler(new ClickHandler() {

                @Override
                public void onClick(ClickEvent event) {

                    selectHandler.onEvent(3);
                    System.out.print("rate 3");
                }
            });

         rb1.addClickHandler(new ClickHandler() {

                @Override
                public void onClick(ClickEvent event) {

                    selectHandler.onEvent(4);
                    System.out.print("rate 4");
                }
            });

         rb1.addClickHandler(new ClickHandler() {

                @Override
                public void onClick(ClickEvent event) {

                    selectHandler.onEvent(5);
                    System.out.print("rate 5");
                }
            });

Presenter class : (event handler)

    private void bind() {
        .
        .
        .

         DoEvent selectHandler = new DoEvent(){
              public void onEvent(int select) {
                  fetchRating(select, user.getUserId());

              }
          };    
          display.setSelectHandler(selectHandler);

The call for the Presenter with it's view, it is called from the MainPagePresenter class :

        presenter = new AssetViewPresenter(rpcService,eventBus,new  AssetView(),result.get(i));

now my problem is that when I click the buttons from the view nothing happens... like the presenter and the view are not connected, what could be the problem ?

Sorry it was a stupid bug... my button are rb0, rb1 ,rb2 ... and I copied the click handlers with the same button id !!! YUP I'm ashamed of myself

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM