Re: callback between classes
On 11/12/2010 3:44 AM, a wrote:
Hi
I have 2 classes. The CatContainer extends the HorizontalPanel. The
CatContainer should be updated when the CatWidget receives updated data
from
database by GWT RPC.
How can I implement the update of the CatContainer?
As mentioned, MVC will help you out here. The idea is to introduce a
third object, the controller, which knows about both objects and can
bind them at run time.
public class InitializationController {
public void init() {
CatContainer model = new CatContainer();
CatWidget view = new CatWidget();
model.addChangeListener( view );
...
}
}
class CatContainer extends HorizontalPanel{
private CatWidget catWidget=new CatWidget(1);
private ModelObserver view;
public void addChangeListener( ModelObserver obs ) {
view = obs;
}
//how can I implement the following to update the GUI?
catWidget.refresh( new CallBackFunc()
{
final CatContainer localModel = CatContainer.this;
onSuccess()
{
//Update GUI
if( localModel.view != null) {
localModel.view.refresh();
}
}
}
}
class CatWidget
implements ModelObserver
{
private data;
void refresh()
{
//Call GWT async to retrieve data from database, and update the data.
}
}
public interface ModelObserver {
void refersh();
}
Nothing here was compiled, double check my syntax.
I don't like the comment you have in "CatWidget". The view should not
know about any database. It should have a simple interface for a model
and that's it. Update the model from the database, then inform the view
that the model has changed. However, I'll leave design issues like that
one to you.