0

I have implemented MVP pattern in my GXT project. The system registers customers as part of it function. Also the system user can search for the registered user providing the id.

i have added an OnClick event to the "search customer" button.

/* On click search button */
        view.getBtnSearch().addListener(Events.OnClick, new Listener<BaseEvent>() {
            @Override
            public void handleEvent(BaseEvent be) {
                eventBus.fireEvent(new CustomerRegistrationTabSelectionEvent(0, false));
                eventBus.fireEvent(new CustomerFetchEvent(view.getValueCustSearchParameter(), view.getValueCustSearchValue(), true));
            }
        });

The CustomerRegistrationTabSelectionEvent does select the relevant tab and enables other tabs. Thats all it does.

Here is the handler for the custom event CustomerFetchEvent.

eventBus.addHandler(CustomerFetchEvent.TYPE, new CustomerFetchEventHandler() {
            @Override
            public void fetchCustomer(CustomerFetchEvent event) {
                searchCustomer(event.getParameter(), event.getParameterValue(), event.isOpenFirstTab()); 
            }
        });

The issue is the search customer method is executed multiple times and if there is a invalid search the error message dialog shows multiple popups. Within the searchCustomer method i call for service which fetch me the customer data or show the popup error message if the search is invalid.

im using GXT 2.2.5 and JRE 1.6.

Could anyone help me in finding out why the code is executed multiple times?

Added Later: When i run the application first time the code is only executed only once, therefore only 1 popup. Then i logout of the system and log in again (navigating to the same page where the "search customer" button exists.) and the code is executed twice. Likewise equal to the number of times i create/navigate to the particular page, the code executes. Is it actually adding the event handler code without removing the last one every time i recreate the page?

2 Answers2

0

Yes, it seems that 'addHandler' adds handler multiple times, but stores previous context. Your code should add handlers only once, on initialization phase. You can check the number of handlers with 'getHandlerCount' method.

udalmik
  • 7,838
  • 26
  • 40
0

Ya. I fixed it!Here is the solution Unbinding presenters necessary in GWT

U can read more here. http://draconianoverlord.com/2010/11/23/gwt-handlers.html

what happened actually was, the presenter objects where i have registered with HandlerManager to receive events were not garbage collected. Because though i remove the reference to the presenters still the HandlerManager holds a reference to those objects. So every time i kept on creating new presenters on top of the old presenters of the same class. so a event is listened by multiple objects of the same class. so u need to ensure that the unused presenters are garbage collected by removing the registered handlers in HandlerManager.

Community
  • 1
  • 1