Tuesday, June 4, 2013

Re: Pb with activity and events (using gin)



On Tuesday, June 4, 2013 12:46:16 PM UTC+2, Tugdual Huertas wrote:
Hi all,

i'm facing a litlle problem using places activities and events... Have tried multiple tricks but not luck .

My problem:
i try to fire an event from the activity but my widget does not catch it since its not loaded (I mean that if i fire this event once the widget's loaded everything works fine).

Here is my Gin configuration:

@Override
protected void configure()
{
   bind(EventBus.class).to(SimpleEventBus.class).asEagerSingleton();
   bind(PlaceHistoryMapper.class).to(MyPlaceHistoryMapper.class).in(Singleton.class);
   bind(ActivityMapper.class).to(MyActivityMapper.class).in(Singleton.class);
   bind(MyActivity.class);
   install(new GinFactoryModuleBuilder().build(MyActivityMapper.Factory.class));
}

@Provides
@Singleton
public PlaceController getPlaceController(EventBus eventBus)
{
   return new PlaceController(eventBus);
}

@Singleton
@Provides
public ActivityManager provideActivityManager(ActivityMapper activityMapper, EventBus eventBus)
{
   return new ActivityManager(activityMapper, eventBus);
}

in MyActivity:
@Inject
private MyWidget myWidget;
@Inject
private EventBus globalEventBus;

@Override
public void start(AcceptsOneWidget panel, EventBus eventBus)
{
   panel.setWidget(myWidget);
   MyEvent event = new MyEvent();
   event.setAction(MyEvent.Action.INIT);
   globalEventBus.fireEvent(event);
}

I also try two different ways of binding events in my widget, during onLoad and in constructor:

During onLoad:

@Inject EventBus eventBus;

@Override
protected void onLoad()
{
   handlersRegistration.add(eventBus.addHandler(MyEvent.TYPE, this));
}


In constructor:

private EventBus eventBus;

@Inject
public MyWidget(EventBus eventBus)
{
   this.eventBus = eventBus;
   // registration of handlers
   handlersRegistration = new ArrayList<HandlerRegistration>();
   // register myself has a listener of different event TYPE
   handlersRegistration.add(eventBus.addHandler(MyEvent.TYPE, this));
   
   ...
}


Am I doing something w rong or missing something?

All adds and removes of event handlers during the dispatching of an event (which is the case when the activity starts: it's in response to a PlaceChangeEvent) is deferred to after the event has been dispatched all handlers.
Events however are dispatched synchronously (so that handlers can possibly modify them and the sender take action depending on the event after the dispatching; see setWarning for PlaceChangeRequestEvent).
So you have to defer your fireEvent() call using the Scheduler:

// Assuming a Scheduler has been @Inject-ed
scheduler.scheduleFinally(new ScheduledCommand() {
  @Override
  public void execute() {
    MyEvent event = new MyEvent();
    event.setAction(MyEvent.Action.INIT);
    globalEventBus.fireEvent(event);
  }
});

It's a good rule to always assume you might be called as part of event dispatching unless you're handling UI events (click on a button, etc.) and thus always defer your event dispatching in this case.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment