Frage

I just finished my first own example with activities/places and MVP. all works fine but some events aren't send or received properly if i change back to a place (from another place). but on "moduleLoad" where this place is set as default place all works fine. i think it shouldn't make a difference if a place/activity is started on moduleLoad (via historyHandler = new PlaceHistoryHandler(historyMapper); historyHandler.register(placeController, eB, defaultPlace); historyHandler.handleCurrentHistory();) or via placeController.goTo(place); , should it?

via debugging i checked the order of event registration, event sending and event receiving (all is executed in start(...) of the activity). the problem is that all receiver don't receive the event if start() is executed via goTo(place) (registration and sending works fine). But if an event is sent after start() or within start()-executed on moduleLoad all works fine!

my activity start looks like that:

@Override
public final void start(final AcceptsOneWidget panel, final EventBus eventBus) {
// register events - to manipulate visibility of some display areas
eventBus.addHandler(SelectedEvent.TYPE, this);
//initiate presenters -(pseudo code)
[presenter that receives SelectedEvent]
[presenter that sends SelectedEvent]
//ading presenter's asWidgets to screen -> panel.setWidget..
...
}

@Override
public final void onSelected(final SelectedEvent event) {
   //do something
}

(the use case for sending this event on start is, that i want say nothing is selected - the event's payload is null)

the problem is that neither the presenter's nor the activity's onSelected -method is called if the start() is called via goTo. But in all cases (checked with debugger) the registering on event occurs before sending it. what should i do?

War es hilfreich?

Lösung

Javascript is not multithread.

When you call a goTo() method, your activities will be started one after the others. If you fire an event inside a start(), the event will be dispatched before the remaining activities are started. So there is a good chance that the activity handling that event has not been started yet (the registration was not done).

You can solve your problem with the following code:

Scheduler.get().scheduleDeferred(new ScheduledCommand()
{
    @Override
    public void execute()
    {
        //Fire the event
    }
});

Scheduler is a utility class provided by GWT. ScheduleDeferred will execute the command after the current browser event loop returns.

So, by pasting this code in a start() , you know the event will be fired as soon as every activity is started.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top