PDA

View Full Version : code in onSuccess method not executed correctly



dark_nights
24 Aug 2009, 1:30 AM
Hello,
I'm doing a simple project showing some contacts from a database in a grid.
I have a method called setGrid(). This one constructs the grid.
In the method onRender(), I'm doing an RPC cal to get the list of contacts in the database.
The problem is that when I call the setGrid method in the onSuccess method, I get a blank page... But when I put the setGrid method before or after the RPC call, the page is shown correctly.
I need to call the setGrid method in order to use the AsyncCallback response.
Help me please, I waste my weekend on this issue.


@Override
protected void onRender(Element parent, int index) {

super.onRender(parent, index);
//setGrid() works here
page.getRpc().getAllContacts(new syncCallback<List<Person>>() {

public void onFailure(Throwable caught) {
System.out.println("pb de rpc");
}

public void onSuccess(List<Person> result) {
System.out.println("OnSuccess : " + result.size());
setGrid() doesn't work here
}
});
// setGrid() works here.
}Thank you

Arno.Nyhm
24 Aug 2009, 3:32 AM
what you do in setGrid()? did you look with firebug if there are some errors or if you debug it did you look in the second (red) gwt log window "Google Web Toolkit Hosted Mode"?

dark_nights
24 Aug 2009, 4:05 AM
Well I'll put the whole code :



package aa.client;

import java.util.ArrayList;
import java.util.List;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.data.BasePagingLoader;
import com.extjs.gxt.ui.client.data.PagingLoader;
import com.extjs.gxt.ui.client.data.PagingModelMemoryProxy;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;

@SuppressWarnings("unchecked")
public class ContactList extends LayoutContainer {

private static int nrows = 0;
private Page page;
private Grid<Person> grid;
private ContentPanel cp;
private PagingModelMemoryProxy proxy;
private PagingLoader loader;
private ListStore<Person> store;

public ContactList(Page p, int rows) {
super();
page = p;
nrows = rows;
cp = new ContentPanel();
}

public ContactList getNewContactList(int rows) {
nrows = rows;
return new ContactList(page, rows);
}


private void setGrid() {
FlowLayout flow = new FlowLayout(nrows);
setLayout(flow);
PagingToolBar toolBar = new PagingToolBar(nrows);//nb de lignes par page
HorizontalPanel hp = new HorizontalPanel();



final ListBox lb = new ListBox();
lb.addItem("10 ");
lb.addItem("20 ");
lb.addItem("30 ");
lb.addItem("40 ");
lb.addItem("50 ");
lb.setSelectedIndex(nrows/10 - 1);

lb.addChangeHandler(new ChangeHandler() {

public void onChange(ChangeEvent event) {

nrows = new Integer(lb.getValue(lb.getSelectedIndex()).trim());
page.applyChangingRowsNumber(nrows);//
}
});


hp.add(new Label(" Montrer "));
hp.add(lb);
hp.add(new Label(" éléments "));

toolBar.insert(hp, 11);

toolBar.bind(loader);

loader.load(0, nrows);

List<ColumnConfig> configs = new ArrayList<ColumnConfig>();

ColumnConfig column = new ColumnConfig();
column.setId("nom");
column.setHeader("Nom<br>");
column.setWidth(200);
configs.add(column);


column = new ColumnConfig();
column.setId("prenom");
column.setHeader("Prenom");
column.setWidth(100);
configs.add(column);

column = new ColumnConfig();
column.setId("compagnie");
column.setHeader("Compagnie");
column.setWidth(100);
configs.add(column);

column = new ColumnConfig();
column.setId("email");
column.setHeader("Email");
column.setWidth(100);
configs.add(column);

column = new ColumnConfig();
column.setId("tel1");
column.setHeader("Tel");
column.setWidth(100);
configs.add(column);

ColumnModel cm = new ColumnModel(configs);

cp.setFrame(true);
cp.setHeading("Contacts");

cp.setButtonAlign(HorizontalAlignment.CENTER);
cp.setLayout(new FitLayout());
cp.setBottomComponent(toolBar);
cp.setSize(700, 380);

grid = new Grid<Person>(store, cm);
grid.setBorders(true);

grid.addListener(Events.RowClick, new Listener<GridEvent<Person>>() {
public void handleEvent(GridEvent<Person> be) {
page.setButtonsEnabled(true);
}
});

grid.addListener(Events.RowDoubleClick, new Listener<GridEvent<Person>>() {

public void handleEvent(GridEvent<Person> be) {
String nom = grid.getView().getCell(be.getRowIndex(), 0).getInnerText();
String prenom = grid.getView().getCell(be.getRowIndex(), 1).getInnerText();
Window.alert("selected nom : " + nom + " & prenom : " + prenom);
}
});
cp.add(grid);
add(cp);
}

private void setProxy(List<Person> result) {
System.out.println("setProxy : " + result.size());
proxy = new PagingModelMemoryProxy(result);
loader = new BasePagingLoader(proxy);
loader.setRemoteSort(true);
store = new ListStore<Person>(loader);
}

@Override
protected void onRender(Element parent, int index) {

super.onRender(parent, index);

page.getRpc().getAllContacts(new AsyncCallback<List<Person>>() {

public void onFailure(Throwable caught) {
System.out.println("pb de rpc");
}

public void onSuccess(List<Person> result) {
System.out.println("OnSuccess : " + result.size());
setProxy(result);
setGrid();
}
});
}
}
setProxy is to initialize the proxy in order to get the list of contacts from the database.
setGrid is to create the grid.
Thanks a lot.

Arno.Nyhm
24 Aug 2009, 4:21 AM
just for understandig: why you will create the grid after the rpc call?

other examples work like this:

create the grid and attach the grid to the window.
assign all loaders and stores.
after this call the rpc

Arno.Nyhm
24 Aug 2009, 4:22 AM
did you look out for some errors in the 2 places?

dark_nights
24 Aug 2009, 4:29 AM
It doesn't matter if I create the grid before or after. Even if I create the grid before the RPC call, I get the same result.
ButI'm wondering why it doesn't work if I out the call to setProxy and setGrid in the onSuccess method, it dosen't work anymore :(