3

I am using GWTP, I am also using NestedPresenter in a form of parentPresenter (Ex: HeaderPage) and childPresenter (ContentPage). I also want to send info from parentPresenter to childPresenter (Ex: when user Loginned in HeaderPresenter then the info will be pass onto the childPresenter).

Now, I am very confusing about the difference among onBind, prepareFromRequest, revealInParent, onReveal, onReset.

Google said (https://code.google.com/p/gwt-platform/wiki/FrequentlyAskedQuestions#How_do_I_choose_between_onReveal()_and_onReset()?) "On First Load, onBind will be called first then prepareFromRequest will be called sencond, revealInParent called 3rd, then onReveal, then onReset".

So here are the list of my questions:

-When a page that have Header & Content, then cycle (onBind, prepareFromRequest, revealInParent, onReveal, onReset) will be called first in parent header presenter, then after that all same cycle (onBind, prepareFromRequest, revealInParent, onReveal, onReset) will be called second in child or nested content presenter?

Or both parentPresenter & childPresenter go through the cycle independently?

If they go through the cycle independently, then we can have a problem. Ex, whenever user refreshes the page (header+content), if the header already checked the session, then I want the header pass the session info into the child content page, but since they go through the cycle independently then how parent presenter pass info when child presenter has not initialized?

Second question is how to know which method should to be put put into which cycle?

Tum
  • 3,614
  • 5
  • 38
  • 63

1 Answers1

4

Your web application can be seen as a tree of Presenters. Walking the tree from bottom to top, onBind will be called on each Presenter. Once all Presenters are bound, onReveal will be called on each Presenter, from the top to the bottom of the tree. The prepareFromRequest method will be called before onReveal.

So, onBind will be called first on ChildPresenter, then onBind will be called on ParentPresenter, then onReveal will be called on ParentPresenter, and finally onReveal will be called on ChildPresenter.

There's no risk that ChildPresenter will not be initialized when ParentPresenter checks the session.

To communicate from the ParentPresenter to the ChildPresenter, I suggest 2 approaches:

  • When the session is fetched by ParentPresenter, ParentPresenter fires a SessionLoadedEvent through the EventBus, and ChildPresenter registers to that event and reacts to it

  • When the session is fetched by ParentPresenter, ParentPresenter calls childPresenter.onSessionFetched(sessionData) directly. ChildPresenter will do whatever he wants with sessionData inside the onSessionFetched method.

You can also see this https://github.com/ArcBees/GWTP/wiki/Presenter-Lifecycle

spg
  • 9,309
  • 4
  • 36
  • 41
  • so i put the method that pass info from parent to child in the onReset of ParentPresenter cos at that time onBind & onReveal already called in childpresenter so there no risk that they r not initialised. Is that right? – Tum Jan 21 '14 at 01:20
  • From what I understand, your ParentPresenter is your app's header. So it's only going to be revealed once (correct me if I'm wrong). Fetching the user's session would be done inside the ParentPresenter's onReaveal method. – spg Jan 21 '14 at 14:05
  • no it's won't work properly. Let say I have page1 (header+content1), page2(header+content2). if i fetch the session into onReveal of parentPresenter in page1, then the code work ok on Page1, but when I open page2, then the header in pager got session info but the content2 did not receive info that should be passed from header in page2 – Tum Jan 21 '14 at 23:53
  • You should store the session info in a User object, that is a Singleton. Just pass that User instance around, and you'll be able to share session state anywhere in your app – spg Jan 22 '14 at 14:50