PDA

View Full Version : GXT MVC and History



mraible
19 Feb 2009, 10:10 AM
I'm using GXT's MVC framework and having success so far. I'm also using DispatcherListeners. My EntryPoint implements HistoryListener and my onHistoryChanged() method looks as follows:



public void onHistoryChanged(String historyToken) {
if (oldHistoryToken != null && historyToken.equals(oldHistoryToken)) return;

// Save the token for the next time round
oldHistoryToken = historyToken;

Dispatcher dispatcher = Dispatcher.get();

if (historyToken != null) {
if (historyToken.equals(HistoryTokens.HOME)) {
dispatcher.dispatch(AppEvents.GoHome);
} else if (historyToken.equals(HistoryTokens.SHOW_LOGIN)) {
dispatcher.dispatch(AppEvents.ShowLogin);
} else if (historyToken.equals(HistoryTokens.SHOW_ADD_CONTACT)) {
dispatcher.dispatch(AppEvents.ShowAddContact);
}
}
}


If I have a Hyperlink like the following.



Hyperlink addContact = new Hyperlink("Add Contact", HistoryTokens.SHOW_ADD_CONTACT);
canvasPanel.add(addContact);

addContact.addClickListener(new ClickListener() {
public void onClick(Widget widget) {
fireEvent(AppEvents.ShowAddContact);
}
});


I've noticed that my controller and DispatcherListener isn't called until onHistoryChanged is called. Is this the recommended way to do things?

If I use dispatcher.dispatch(AppEvents.ShowAddContact), my DispatcherListener is called twice.

Thanks,

Matt

sdc
19 Feb 2009, 10:27 AM
I'm not sure to understand. It would be easier if you post sample code as explained here : http://extjs.com/forum/showthread.php?t=40289

I just wonder about the fireEvent call. I guess it is in a View. Do you already know that fireEvent will only fire the event to the owning Controller and its children (and not to every controller that you have add in the Dispatcher).

mraible
19 Feb 2009, 12:31 PM
I'm not sure to understand. It would be easier if you post sample code as explained here : http://extjs.com/forum/showthread.php?t=40289

I just wonder about the fireEvent call. I guess it is in a View. Do you already know that fireEvent will only fire the event to the owning Controller and its children (and not to every controller that you have add in the Dispatcher).

No, I did not know that fireEvent only calls the owning Controller. We were previously using dispatch(AppEvents.EventName), but noticed that our DispatchListeners were being called twice (once after dispatch and once after history changed).

Do we need to implement the HistoryListener in our EntryPoint? The Mail example doesn't seem to do this.

sdc
20 Feb 2009, 2:01 AM
noticed that our DispatchListeners were being called twice (once after dispatch and once after history changed).

When are they called twice : when you click on the hyperlink ?

mraible
20 Feb 2009, 9:28 AM
Yes, when I click the link. If I use fireEvent in a View, the controller isn't called (likely because it's a different controller not associated with the view). However, it is called by my EntryPoint in the onHistoryChanged() method.

sdc
20 Feb 2009, 11:03 AM
Ok so if you call dispatcher.dispatch your listeners are called twice, if you use fireEvent they are called once, that's it ?
If that's it, here is the explanation : when you click on your hyperlink, it calls the dispatch (because you have added a click listener) AND then onHistoryChanged is called and the dispatch is called a second time.

By the way, instead of using Hyperlinks, you can also use buttons or any other widget and put the history token in the AppEvent :


Button addContact = new Button("Add Contact", new SelectionListener<ButtonEvent>(){
@Override
public void componentSelected(ButtonEvent ce) {
AppEvent event = new AppEvent(AppEvents.ShowAddContact,null,HistoryTokens.SHOW_ADD_CONTACT);
dispatcher.dispatch(event);
}
});
canvasPanel.add(addContact);