Tuesday, January 29, 2013

Re: More Questions about MVP, Custom Events and the Eventbus

You are not forced to use an EventBus. You could write a presenter/controller that keeps track of the selected view and changes its style through a view method.

Something like:

interface RedBorderView extends IsWidget {

  //UI events are delegated to meaningful methods
  interface Delegate {
     //contains the source of the event as interface so it can be mocked/stubbed for testing and you have access to its methods.
     //Method does not publish the fact that selection is done through 'clicking' (maybe its 'touching' on a mobile device)
     onRedBorderViewSelected(RedBorderView selected);
  }

  //tell the view who processes the available UI events
  void setDelegate(Delegate delegate);

  //change style based on state
  void setSelected(boolean selected);

}

//if you want to hide the delegate methods on MyController you can also use a private inner class that implements the Delegate interface.
class MyController implements RedBorderView.Delegate {

  RedBorderView selected;

  void createRedBorderViews() {
     for(....) {
       view.setDelegate(this);
     }
  }

  void onRedBorderViewSelected(RedBorderView selected) {
    this.selected.setSelected(false);
    this.selected = selected;
    this.selected.setSelected(true);
  }

}

I think you got the idea. 

If your set of custom widgets represent something larger that you want to reuse it then its maybe better to write a SomethingLargerWidget that internally uses a controller/presenter to keep track of all the RedBorderViews and its state. Then you would directly use your SomethingLargerWidget just like any other GWT widget.

Personally I use EventBus only for "application events", something like Login/LogoutEvent, Busy/IdleEvent, BuddyOnline/OfflineEvent etc. So basically for events that totally different classes may be interested in.

But if you want to use an EventBus I think its fine to use it directly in the view as long as the logic behind it is so simple that you are not planning to unit test it anyways. If you need to test the logic behind firing your custom event then dont put it into the view implementation because then you have to use GWTTestCase which is super slow.


-- J.

--
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 google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment