Saturday, July 2, 2011

MVP best practices, how to expose a view to a presenter?

Hi guys,

I've been trying to find the best answer to the question above for a while now. Here is the problem:

* assume you have a view with textbox and a vertical panel. The textbox is used for searching, the vertical panel to display the found results. 
In order to decouple everything, we'll define 2 interfaces:

interface ISearchView {
  void populateResults (List<String> results);
  setPresenter (ISearchPresenter p);
}

interface ISearchPresenter {
  void doSearch (String query);
}

* using the code above, I can safely bind events in view, that dispatch business execution to a presenter (set via #setPresenter). Everything is very clean & organized.

However, when I'm implementing an Activity, here is how it would look like:

class SearchActivity extends AbstractActivity implements ISearchPresenter {

@Override
public void start(final AcceptsOneWidget panel, EventBus eventBus) {
           ISearchView view = clientFactory.getSearchView();
           view.setPresenter (this);
           panel.setWidget (view);
        }

        @Override
        public void onSearch (String query) {
             // Execute some sync/async call.
             clientFactory.searchService().search (query, .......);

             // HOW DO I SEND RESULTS BACK TO VIEW??
        }
}


I don't know how to access the view instance, in order to populate it with some results. One way, which I honestly think it's uberly wrong -- is to access it through clientFactory.getSearchView. But this basically forces me to have a singleton scope of that view. Not acceptable, imo.

The example http://code.google.com/webtoolkit/doc/latest/DevGuideMvpActivitiesAndPlaces.html doesn't include this case (by coincidence or ...)

Thanks.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/ODhMpq8LQOgJ.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment