tags:

views:

119

answers:

2

I recently tried to follow the Large scale application development and MVP tutorial. The tutorial was great but I am having a hard time with a few things.

If you try and add a contact to the list, the contact is created. If you try and add another contact, you are taken to the edit screen of the last contact you created. No more contacts can be added once you add your first contact. What needs to be changed so you can add more than one contact.

Changes I have made to try and get it to work:

Create a new editContactsView each time the add button is pressed. This brings up a blank edit screen, but the new contact still overwrites the previous addition.

Changed contacts.size() to contacts.size()+1 when determining the ID of the new contact.

A: 

When a new contact is added it's id is never set. Because the id field is a string it is stored as "". That is how the first contact is added. Now every time you create a new contact you overwrite the contact with key "". To fix this you need to set the value of the id. I did this by changing the doSave method in EditContactsPresenter.

private void doSave() {
contact.setFirstName(display.getFirstName().getValue());
contact.setLastName(display.getLastName().getValue());
contact.setEmailAddress(display.getEmailAddress().getValue());
if(History.getToken.equals("add")
    rpcService.updateContact(contact, new AsyncCallback<Contact>() {        
        public void onSuccess(Contact result) {          
            eventBus.fireEvent(new ContactUpdatedEvent(result));        
        }        
        public void onFailure(Throwable caught) {          
            Window.alert("Error updating contact");        
        }    
    });  
else    
    rpcService.updateContact(contact, new AsyncCallback<Contact>() {        
        public void onSuccess(Contact result) {          
            eventBus.fireEvent(new ContactUpdatedEvent(result));        
        }        
        public void onFailure(Throwable caught) {          
            Window.alert("Error updating contact");        
        }    
    });  
}
Lumpy
A: 

Actually, there are a couple of problems (from what I can see):

  • like Lumpy already mentioned, the new Contact created via EditContactPresenter doesn't get an id assigned (it's null). This is because EditContactPresenter uses the default Contact() constructor which doesn't set the id. There are many possible solutions to this: add setting the id in the default constructor (so that you don't have to keep track of the ids somewhere else in the app), delegate that function to your server (for example, make your DB generate the next available id and send it back) or just add a contact.setId(whatever); in the appropriate place in EditContactsPresenter
  • AppController.java:134 - this example reuses the view (which is a good idea), but it doesn't clear it if you use it for creating a new Contact. Solution: either disable view reusing (just make a new EditContactsView every time) or add a clear() or sth similar to your Views and make the Presenters call it when they want to create a new entry, instead of editing an exisiting one (in which case, the values from the current entry overwrite the old values, so it's ok).

It's weird that this sample was left with such bugs - although I understand that it's main purpose was to show how MVP and GWT go together, but still :/

Igor Klimer