Thank you so much, Thomas. Your solution is very helpful. Though I still
have three more questions:
1) In the comment to your code, it's said that a Scheduler must be also
managed by GIN. Therefore, the question if it is a bad practice to use
Scheduler.get(), while the rest of the project is managed by GIN? How about
RootPanel.get() (or RootLayoutPanel.get()) factory calls? Seems like one
must replace all no-parameter calls of that kind with injections. Correct
me if I'm wrong.
2) Back to the main subject about EventBus. Actually I tried
scheduler.scheduleDeferred()before I started looking for solutions. Seems
like only scheduler.scheduleFinally() defers the event firing long enough
to give other activities time to append their handlers withing start()
method. At least in my particular case I encountered the "racing" condition
between activities for header, content and other sections of my layout.
Therefore, while there's no parallel computation in browser, GWT approach
with deferred tasks adds some unexplainable magic to the process of
debugging. I'm not even sure if I ever gonna prefer scheduleDeferred() to
scheduleFinally() from now on.
3) I still need to fire events manually sometimes. Therefore I do inject
EventBus instance to some of the activities. Despite the fact start()
method uses different instance of EventBus, the code still works fine like
if it's all one single instance. This discovery confuses me a lot, because
buses are clearly different. How is it it even possible? And what might be
the best practice of using EventBus with Activities&Places + GIN?
On Tuesday, June 4, 2013 3:06:28 PM UTC+4, Thomas Broyer wrote:
>
>
>
> 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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.