The problem is large-scale application development and the MVP tutorial

I recently tried to do a large-scale application development and MVP tutorial . The tutorial was great, but it's hard for me to do something.

If you try to add a contact to the list, a contact will be created. If you try to add another contact, you get to the edit screen of the last contact you created. You cannot add more contacts after adding the first contact. What needs to be changed so that you can add multiple contacts.

The changes I made to try to make it work:

Create a new editContactsView each time you click the add button. This brings up a blank editing screen, but the new contact still overwrites the previous add-on.

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

+4
source share
2 answers

In fact, there are several problems (from what I see):

  • As already mentioned by Lumpy, a new Contact created using EditContactPresenter does not receive the assigned identifier (it null ). This is because EditContactPresenter uses the default Contact() constructor, which does not set the identifier. There are many possible solutions: add the id parameter to the default constructor (so that you do not have to track identifiers somewhere else in the application), delegate this function to your server (for example, make your database generate the next available identifier and send it back) or just add contact.setId(whatever); to the appropriate place in EditContactsPresenter
  • AppController.java:134 - this example repeats the view (which is a good idea), but it does not clear it if you use it to create a new Contact . Solution: Either turn off reuse reuse (just create a new EditContactsView each time), or add clear() or sth, similar to your views, and make speakers call it when they want to create a new record, instead of editing the existing one (in this case, the values ​​from the current record overwrites the old values, so this is normal).

It is strange that this sample remained with such errors - although I understand that the main goal was to show how MVP and GWT go together, but still: /

+6
source

When adding a new contact, the identifier is never set. Since the id field is a string, it is saved as ". This adds the first contact. Now, every time you create a new contact, you overwrite the contact with the" "key. To fix this, you need to set the id value. I did this by changing the method doSave 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"); } }); } 
0
source

All Articles