我正在使用GWT 2.4使用MVP模式
我遇到这种情况: 在一个演示者(让我们称之为TreePresenter,因为它显示一棵树)我有以下代码:
display.getSelectedItem().addSelectionHandler(new SelectionHandler<TreeItem>() {
@Override
public void onSelection(SelectionEvent<TreeItem> event) {
....
evtBus.fireEvent(new SelectCategoryEvent(item.getText()));
.....
}
});
在我的AppController类中,我有这种情况:
eventBus.addHandler(SelectCategoryEvent.TYPE, new SelectCategoryEventHandler() {
@Override
public void onSelectCategory(SelectCategoryEvent event) {
saveHistoryEvent(event);
}
});
当我在三个中选择一个itm时,指令
正确触发了该事件evtBus.fireEvent(new SelectCategoryEvent(item.getText()));
但是在我的AppController中,事件没有传播,我无法在代码中处理它
eventBus.addHandler(SelectCategoryEvent.TYPE, new SelectCategoryEventHandler() {
@Override
public void onSelectCategory(SelectCategoryEvent event) {
saveHistoryEvent(event);
}
});
有人可以告诉我原因吗?
感谢 安吉洛
我会给你一些细节;我为自己的历史管理建了一堂课;我建了这个课:
public class GwtHistoryEventsMgr {
private Map<String, List<GwtEvent>> tknEvts;
private HandlerManager eventBus;
public GwtHistoryEventsMgr(HandlerManager evtBus){
tknEvts = new HashMap<String, List<GwtEvent>>();
this.eventBus = evtBus;
}
private void saveHistoryEvent( GwtEvent event ){
List<GwtEvent> eventi = null;
if( tknEvts.containsKey(History.getToken()) ){
eventi = tknEvts.get(History.getToken());
}else{
eventi = new ArrayList<GwtEvent>();
}
eventi.add(event);
tknEvts.put(History.getToken(), eventi);
}
public void addEvtHandlers(){
//Aggiungo gli handler
eventBus.addHandler(CustomEvent.TYPE, new CustomEventHandler() {
@Override
public void onEvent(CustomEvent event) {
saveHistoryEvent(event);
}
});
}
public List<GwtEvent> getTokenTransWidgetEvents(String token){
if( tknEvts.containsKey(token) ){
return tknEvts.remove(token);
}else{
return null;
}
}
}
然后在我的AppController构造函数中,我写了这段代码:
public AppController(HandlerManager eventBus, StandardDispatchAsync dispatcher){
super(null);
this.eventBus = eventBus;
this.dispatcher = dispatcher;
//Gestione history
histMgr = new GwtHistoryEventsMgr(eventBus);
histMgr.addEvtHandlers();
}
这意味着我应该非常确定AppController将自己注册到我想要的事件(注意:我强烈减少了代码......但所有代码都是我写的方式)
然后,由于我使用客户端反射,我做了,我使用客户端反射,这个代码(之后所有小部件都已初始化):
@SuppressWarnings("rawtypes")
private void generateHistoricalEvents(){
List<GwtEvent> eventi = hisMgr.getTokenTransWidgetEvents(History.getToken());
//Ci sono eventi....vuol dire back del browser cliccato
if( eventi != null ){
for (GwtEvent gwtEvent : eventi) {
this.eventBus.fireEvent(gwtEvent);
}
}
}
据我所知,一切都好;你能告诉我问题出在哪里吗?
谢谢 安吉洛