Monday, December 21, 2015

GWT Asynchronous callback and UI

Hello all,

      I have a Composite with Widgets that are populated from the database.  Let's assume I need to add new Item object and ID for this object comes from database after object is persisted on the server. So I did:

     

private void addItem(String title, String description) {
         
ItemDTO itemDto = new ItemDTO(0L, title, description, false);
          todoService
.saveItem(itemDto, new AsyncCallback<ItemDTO>() {
           
@Override
           
public void onFailure(Throwable caught) {
               
Window.alert("Failed to save item");
           
}
           
@Override
           
public void onSuccess(ItemDTO itemDto) {
               
Window.alert("Item saved");
                item
.setId(itemDto.getId());
                item
.setTitle(itemDto.getTitle());
                item
.setDescription(itemDto.getDescription());
                content
.add(item);
           
}
           
       
});

      
       This way Item is added to database, but Window.alert is not shown and UI does not show the item newly added.
       If I did this way:
 

private void addItem(String title, String description) {
       
Item item = new Item();
        item
.setTitle(title);
        item
.setDescription(description);
        content
.add(item);
       
ItemDTO itemDto = new ItemDTO(0L, title, description, false);
        todoService
.saveItem(itemDto, new AsyncCallback<Long>() {
           
@Override
           
public void onFailure(Throwable caught) {
               
Window.alert("Failed to save item");
           
}

           
@Override
           
public void onSuccess(Long id) {
               
Window.alert("Item saved");
                updateItem
(id);
           
}

       
});
   
}

   
private void updateItem(Long id) {
       
Item item = (Item) content.getWidget(content.getWidgetCount()-1);
        item
.setId(id);
   
}

         Everything works as expected, but "update" item logic is a bit dangerous. Why I can't add item object in callback?  What's wrong with a first example?

Thank you.

--
You received this message because you are subscribed to the Google Groups "GWT Users" 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 https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment