[errai-dev] Declarative API for PropertyChangeHandlers in DataBinding

Mike Brock cbrock at redhat.com
Mon Mar 11 20:33:39 EDT 2013


While I was playing tonight I had a few ideas I wanted to bounce off everyone. What about a higher level API for handling change events?

Consider the following:

====HelloWorld.java=========================================

@EntryPoint
@Templated("#root")
public class HelloWorld extends Composite {
  @Inject @AutoBound private DataBinder<MyModel> model;

  @Inject @NeedsData @Bound @DataField private TextBox name;
  @Inject @DataField private Button button;

  @EventHandler("button")
  private void onClick(ClickEvent e) {
    messageBox.setText("Hello there, " + model.getModel().getName());
  }

   @OnModelChange
   public void onMyModelChange(@OldModel     MyModel model,
                                                             @NewModel   MyModel newModel,
 							     @Property   String propertyName,
                                                             @Source     Object source) {
       // react to change
   } 
}

----------------------------------------------------------------------------------------------------

====ChangeEvents.java======================================

@Singleton
public class ChangeEvents [
   @OnGlobalModelChange
   public void onMyModelChange(@OldModel     MyModel model,
                                                             @NewModel   MyModel newModel,
 							     @Property   String propertyName,
                                                             @Source     Object source) {
       // react to change
   } 
}

----------------------------------------------------------------------------------------------------

Note: the idea is that each of the specified attributes are optional with the exception you must specify *at least* @OldModel or @NewModel. So in practice you might just do something like this:

public void onMyModelChange(@NewModel MyModel updatedModel) {
   // do something with updatedModel
}

The general idea is that the @ OnGlobalModelChange would match all *managed* (read: injected automatically by the container such as @AutoBound) DataBinders that match MyModel. Where-as the @OnModelChange in the HelloWorld class would be scoped just to that one ErraiUI bean. 

----

This is just something I brainstormed in about 15 minutes. I am not fixed on this particular approach. But I found myself wanting a declarative way of listening for changes tonight that didn't involve me adding a @PostConstruct and manually adding a PropertyChangeHandler to the DataBinder. 









More information about the errai-dev mailing list