MVP Contacts example with a different flavor

July 31, 2010 at 7:37 pm Leave a comment

In the previous post, I went thru GWT’s MVP Contacts example (2.0.x).  Now, I’ll take a look at the Contacts MVP example with Pectin thrown into the mix.   

The Pectin site mentions the differences between MVP and Pectin’s Presentation Model:

The original Contacts Display Interface:

public interface Display {
HasClickHandlers getSaveButton();
HasClickHandlers getCancelButton();
HasValue getFirstName();
HasValue getLastName();
HasValue getEmailAddress();
Widget asWidget();
}

The Pectin Contacts Display Interface where a single domain object setter is used instead of individual HasValue getters:

public interface Display
{
HasClickHandlers getSaveButton();
HasClickHandlers getCancelButton();
void setContact(Contact contact);
void commit(boolean resetDirty);
boolean validate();
Widget asWidget();
}

Note, the commit() and validate() methods have been added to the Pectin Display interface .

The original editContactPresenter constructor
public EditContactPresenter(ContactsServiceAsync rpcService, HandlerManager eventBus, Display display) {
this.rpcService = rpcService;
this.eventBus = eventBus;
this.contact = new Contact();
this.display = display;
bind();
}

The Pectin editContactPresenter constructor where the Display interface is given the Contact domain object:

public EditContactPresenter(ContactsServiceAsync rpcService, HandlerManager eventBus, Display display)
{
this.rpcService = rpcService;
this.eventBus = eventBus;
this.display = display;
this.contact = new Contact();
display.setContact(contact);
bind();
}

Similarly, for the original Contact constructor where an rpc call is performed to get the Contact domain data:


rpcService.getContact(id, new AsyncCallback() {
public void onSuccess(Contact result) {
contact = result;
EditContactPresenter.this.display.getFirstName().setValue(contact.getFirstName());
EditContactPresenter.this.display.getLastName().setValue(contact.getLastName());
EditContactPresenter.this.display.getEmailAddress().setValue(contact.getEmailAddress());
}

For the pectin Contact constructor where an rpc call is performed to get the Contact domain data where the Display interface is given the Contact domain object:


rpcService.getContact(id, new AsyncCallback()
{
public void onSuccess(Contact result)
{
EditContactPresenter.this.contact = result;
EditContactPresenter.this.display.setContact(result);
}

Now for the original doSave method which persists the Contact data:

private void doSave() {
contact.setFirstName(display.getFirstName().getValue());
contact.setLastName(display.getLastName().getValue());
contact.setEmailAddress(display.getEmailAddress().getValue());
rpcService.updateContact(contact, new AsyncCallback() {
public void onSuccess(Contact result) {
eventBus.fireEvent(new ContactUpdatedEvent(result));
}
public void onFailure(Throwable caught) {
Window.alert("Error updating contact");
}
});
}

The Pectin doSave method which persists the Contact data clearing the dirty bit after a successful RPC call :

private void doSave()
{
if (display.validate())
{
// save changes to the contact, but not clear the contact dirty flag
display.commit(false);

rpcService.updateContact(contact, new AsyncCallback()
{
public void onSuccess(Contact result)
{
display.commit(true);
eventBus.fireEvent(new ContactUpdatedEvent(result));
}

public void onFailure(Throwable caught)
{
Window.alert(“Error updating contact”);
display.revert();
}
});
}
}

In Pectin, bindings allow you to easily connect a property in a domain object to a widget (in the view) so that a change in either of them is automatically synchronized to the other. More specifically, when you bind it to widgets, the binder binds FieldModel instances to any HasValue widget.

In the example, class EditContactModel is created which inherits from Pectin’s FormModel. In this class, FieldModel objects are created which correspond to the widgets:
protected final FieldModel firstName;
protected final FieldModel lastName;
protected final FieldModel emailAddress;

As well, we define a Pectin Provider which is referenced as folllows:
private BeanModelProvider contactProvider;
this((ContactProvider) GWT.create(ContactProvider.class));

Lastly, in our view we bind the FormModel properties to the widgets:

binder.bind(model.firstName).to(firstName);
binder.bind(model.lastName).to(lastName);
binder.bind(model.emailAddress).to(emailAddress);

Advertisements

Entry filed under: Uncategorized.

MVP Contacts Stockwatcher Google App Engine experience

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


@davidmbloom


%d bloggers like this: