bind(MyClass.class).asEagerSingleton();
..but it does not seem to work. When I launch my GWT app, only the
main class is loaded. None of the constructors from my other classes
are called and I need them to be since they listen for events.
because the childPresenterA and childPresenterB references have not
been injected yet. I suppose I could move them into the constructor,
but is there any alternative recommendation here?
public void onModuleLoad() {
injector.getEventBus().fireEvent(new PlaceRequestEvent(new
PlaceRequest(LoginPresenter.PLACE)));
injector.getPlaceManager().fireCurrentPlace();
History.newItem(LoginPresenter.PLACE.toString());
}
So, instead of manipulating the root panel directly, I just fire an
event and the LoginPresenter will end up getting that, and he'll
manipulate the view. The problem is that the event fires, and the
LoginPresenter gets it, and *now* he gets a NPE on this method:
protected void onPlaceRequest(PlaceRequest request) {
System.out.println("LOGIN PLACE REQUEST");
RootPanel.get("header").clear();
RootPanel.get("header").add(lhPresenter.getDisplay().asWidget());
<-- NPE
RootPanel.get("button-row").clear();
RootPanel.get("content").clear();
RootPanel.get("content").add(display.asWidget());
}
His ctor looks like this:
@Inject
public LoginPresenter(
LoginPresenter.Display display,
EventBus eventBus,
LoginHeaderPresenter lhPresenter) {
super(display, eventBus);
this.lhPresenter = lhPresenter;
bind();
}
When is it safe to assume that all the classes have been constructed
and their dependencies are injected proper? I'm either making
incorrect assumptions about the way it should behave, or perhaps there
is a bug here somewhere?
All presenters get an instance of the eventbus injected via the
constructor, and they register a specific type of event handler with
it. This is how I am managing place/history.
Example:
public class Presenter {
public interface Display {
/* show a model object */
void showSomething(Model model);
}
@Inject
public Presenter(Presenter.Display display, EventBus bus) {
this.display = display;
this.bus = bus;
bus.registerEventHandler(PlaceRequestEvent.getType(), new
PlaceRequestHandler() { ... });
}
public void onPlace(PlaceRequestEvent evt) {
if(evt.getPlace().equals(me) {
// change the view
}
}
}
The bindings would thus be:
bind(EventBus.class).to(DefaultEventBus.class).in(Singleton.class);
bind(Presenter.Display.class).to(PresenterView.class);
bind(Presenter.class).asEagerSingleton();
The LoginPresenter also has a LoginHeaderPresenter in the ctor:
public LoginPresenter(Display display, EventBus bus, LoginPresenter
presenter) { }
when the event fires from onModuleLoad( ) -->
injector.getEventBus().fireEvent(new PlaceRequestEvent(new
PlaceRequest(LoginPresenter.PLACE)));
The LoginPresenter will get that event and try to manipulate the root panel:
protected void onPlaceRequest(PlaceRequest request) {
RootPanel.get("header").clear();
RootPanel.get("header").add(presenter.getDisplay().asWidget());
RootPanel.get("content").clear();
RootPanel.get("content").add(display.asWidget());
}
But when that method is called -- the LoginPresenter's reference to
LoginHeaderPresenter (which should have been injected in ctor) is
null, and results in NPE.
I will see what I can do about creating a simple project example that
demonstrates this, but I'd love to hear any ideas on what might be
going wrong. I will change my bindings to .in(Singleton.class) for
presenters to see if it changes.
LoginPresenter ctor looks like this:
@Inject
public LoginPresenter(
LoginPresenter.Display display,
EventBus eventBus,
LoginHeaderPresenter lhPresenter) {
super(display, eventBus);
this.lhPresenter = lhPresenter;
bind();
}
super class registers place request event handler for us.
Ginjector fires the event in onModuleLoad:
injector.getEventBus().fireEvent(new PlaceRequestEvent(new
PlaceRequest(LoginPresenter.PLACE)));
which ends up calling back into LoginPresenter#onPlaceRequest( )
which tries to de-reference lhPresenter (injected via ctor), but this is null...
Hope that is more clear?
On Tue, Aug 25, 2009 at 10:31 PM, Davis
Ford<davi...@zenoconsulting.biz> wrote:
> Hi Peter,
>