I am using mvp4g in my gwt project. For one of my presenters I am using option multiple=true and I am creating and binding presenters in that way:
ObjectPresenter mainObject = eventBus.addHandler(ObjectPresenter.class, false);
mainObject.setId(id);
mainObject.bind();
view.addWidget(mainObject.getView().asWidget());
ObjectPresenter extends LazyPresenter.
When I am calling first event from the eventBus that is caught by ObjectPresenter, method bind() of the LazyPresenter is called again.
bind method has inside tree other methods: createPresenter(); view.createView(); bindView();. In the bindView method of the ObjectPresenter I am modifing my view by adding next widgets. Because the method is called twice (once directly by me, and once by framework) some widgets are duplicated.
I've debugged the code and I found that this part of code from BaseEventHandler is called when the event from the eventBus is called:
public final boolean isActivated( boolean passive, String eventName, Object... parameters ) {
boolean activated = this.activated && pass( eventName, parameters );
if ( activated ) {
if ( passive ) {
return binded;
} else {
onBeforeEvent();
if ( !binded ) {
bind();
binded = true;
}
}
}
return activated;
}
After calling bind directly (by mainObject.bind()) binded property in the BaseEventHandler is not set to true, so bind method is called again when first event is called.
I can set binded variable from the BaseEventHandler to true in the ObjectPresenter when the method bind (called directly) is finished, but I am not sure if it is proper approach...
Could you please give me a hint how to deal with this issue?
Thanks for your help.
I haven't read documentation carefully enough! I was using method
eventBus.addHandler(ObjectPresenter.class, false)(withbindparameter), which has following java doc.The part I missed is When binding the handler, you have to call the isActivated method. This method will be called with eventName and parameters set to null. and that was my problem - I didn't do this!