一个AppEvent不仅仅可以被一个Controller所捕捉,它可以被多个Controller所捕捉,关键在于有多少个Controller注册了该AppEvent。
下面我们要结合之前代码,新建一个新的StatusController,注册之前已有的Events,使用StatusToolbar component来给用户显示系统的运行状态。
我们现在针对两个以后的事件:
下面具体实现步骤如下:
public static final EventType StatusToolbarReady = new EventType();
package com.danielvaughan.rssreader.client.mvc.controllers; import com.danielvaughan.rssreader.client.mvc.events.AppEvents; import com.danielvaughan.rssreader.client.mvc.views.StatusView; import com.extjs.gxt.ui.client.mvc.AppEvent; import com.extjs.gxt.ui.client.mvc.Controller; public class StatusController extends Controller { private StatusView statusView; public StatusController() { registerEventTypes(AppEvents.Init); registerEventTypes(AppEvents.Error); registerEventTypes(AppEvents.UIReady); registerEventTypes(AppEvents.FeedSelected); registerEventTypes(AppEvents.ItemSelected); } @Override public void handleEvent(AppEvent event) { forwardToView(statusView, event); } @Override public void initialize() { super.initialize(); statusView = new StatusView(this); } }
package com.danielvaughan.rssreader.client.mvc.views; import com.extjs.gxt.ui.client.mvc.AppEvent; import com.extjs.gxt.ui.client.mvc.Controller; import com.extjs.gxt.ui.client.mvc.View; public class StatusView extends View { public StatusView(StatusController statusController) { super(statusController); } @Override protected void handleEvent(AppEvent event) { } }
private final Status status = new Status(); private final ToolBar toolBar = new ToolBar(); public void setStatus(String message) { status.setText(message); }
private void onInit() { status.setWidth("100%"); status.setBox(true); toolBar.add(status); Dispatcher.forwardEvent(new AppEvent(AppEvents.StatusToolbarReady, toolBar)); }
@Override protected void handleEvent(AppEvent event) { EventType eventType = event.getType(); if (eventType.equals(AppEvents.Init)) { onInit(); setStatus("Init"); } }
public AppController() { registerEventTypes(AppEvents.Init); registerEventTypes(AppEvents.Error); registerEventTypes(AppEvents.UIReady); registerEventTypes(AppEvents.NavPanelReady); registerEventTypes(AppEvents.FeedPanelReady); registerEventTypes(AppEvents.ItemPanelReady); registerEventTypes(AppEvents.StatusToolbarReady); }
private void onStatusToolbarReady(AppEvent event) { Component component = event.getData(); mainPanel.setBottomComponent(component); }
@Override protected void handleEvent(AppEvent event) { EventType eventType = event.getType(); if (eventType.equals(AppEvents.Init)) { onInit(event); } else if (eventType.equals(AppEvents.Error)) { onError(event); } else if (eventType.equals(AppEvents.UIReady)) { onUIReady(event); } else if (eventType.equals(AppEvents.NavPanelReady)) { onNavPanelReady(event); } else if (eventType.equals(AppEvents.FeedPanelReady)) { onFeedPanelReady(event); } else if (eventType.equals(AppEvents.ItemPanelReady)) { onItemPanelReady(event); }else if (eventType.equals(AppEvents.StatusToolbarReady)) { onStatusToolbarReady(event); } }
@Override public void onModuleLoad() { final FeedServiceAsync feedService = GWT.create(FeedService.class); Registry.register(RSSReaderConstants.FEED_SERVICE, feedService); Dispatcher dispatcher = Dispatcher.get(); dispatcher.addController(new AppController()); dispatcher.addController(new NavController()); dispatcher.addController(new FeedController()); dispatcher.addController(new ItemController()); dispatcher.addController(new StatusController());//here dispatcher.dispatch(AppEvents.Init); dispatcher.dispatch(AppEvents.UIReady); }
@Override protected void handleEvent(AppEvent event) { EventType eventType = event.getType(); if (eventType.equals(AppEvents.Init)) { onInit(); setStatus("Init"); } else if (eventType.equals(AppEvents.FeedSelected)) { Feed feed = event.getData(); setStatus("Feed Selected - (" + feed.getTitle() + ")"); } }
@Override protected void handleEvent(AppEvent event) { EventType eventType = event.getType(); if (eventType.equals(AppEvents.Init)) { onInit(); setStatus("Init"); } else if (eventType.equals(AppEvents.FeedSelected)) { Feed feed = event.getData(); setStatus("Feed Selected - (" + feed.getTitle() + ")"); } else if (eventType.equals(AppEvents.ItemSelected)) { Item item = event.getData(); setStatus("Item Selected - (" + item.getTitle() + ")"); } }