下面,按照同样的逻辑,去处理FeedPanel区域。
public static final EventType FeedPanelReady = new EventType();
package com.danielvaughan.rssreader.client.mvc.controllers; import com.extjs.gxt.ui.client.mvc.AppEvent; import com.extjs.gxt.ui.client.mvc.Controller; public class FeedController extends Controller { @Override public void handleEvent(AppEvent event) { } }
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 FeedView extends View { public FeedView(Controller controller) { super(controller); } @Override protected void handleEvent(AppEvent event) { } }
public FeedController() { registerEventTypes(AppEvents.Init); }
package com.danielvaughan.rssreader.client.mvc.controllers; import com.danielvaughan.rssreader.client.mvc.events.AppEvents; import com.danielvaughan.rssreader.client.mvc.views.FeedView; import com.extjs.gxt.ui.client.mvc.AppEvent; import com.extjs.gxt.ui.client.mvc.Controller; public class FeedController extends Controller { private FeedView feedView; public FeedController() { registerEventTypes(AppEvents.Init); } @Override public void handleEvent(AppEvent event) { forwardToView(feedView, event);//同样的,将接收到的event转发到feedView里 } @Override public void initialize() { super.initialize(); feedView = new FeedView(this); } }
private final FeedPanel feedPanel = new FeedPanel();
@Override protected void handleEvent(AppEvent event) { EventType eventType = event.getType(); if (eventType.equals(AppEvents.Init)) { onInit(event); } } private void onInit(AppEvent event) { Dispatcher.forwardEvent(new AppEvent(AppEvents.FeedPanelReady, feedPanel)); }
public AppController() { registerEventTypes(AppEvents.Init); registerEventTypes(AppEvents.Error); registerEventTypes(AppEvents.UIReady); registerEventTypes(AppEvents.NavPanelReady); registerEventTypes(AppEvents.FeedPanelReady); }
@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); } } private void onFeedPanelReady(AppEvent event) { RowData rowData = new RowData(); rowData.setHeight(.5); Component component = event.getData(); mainPanel.add(component, rowData); }
package com.danielvaughan.rssreader.client; import com.danielvaughan.rssreader.client.mvc.controllers.AppController; import com.danielvaughan.rssreader.client.mvc.controllers.FeedController; import com.danielvaughan.rssreader.client.mvc.controllers.NavController; import com.danielvaughan.rssreader.client.mvc.events.AppEvents; import com.danielvaughan.rssreader.client.services.FeedService; import com.danielvaughan.rssreader.client.services.FeedServiceAsync; import com.extjs.gxt.ui.client.Registry; import com.extjs.gxt.ui.client.mvc.Dispatcher; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.core.client.GWT; /** * Entry point classes define <code>onModuleLoad()</code>. */ public class RSSReader implements EntryPoint { /** * This is the entry point method. */ @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.dispatch(AppEvents.Init);会派发Init事件,虽然只是执行了一次派发操作,但是会派发到多个controller中去! // 原因:因为AppController和NavController都注册了Init ! // 顺序:两个controller的接收到event的顺序是根据上面的两行代码(controller的加入顺序)有关! dispatcher.dispatch(AppEvents.Init); dispatcher.dispatch(AppEvents.UIReady); } }
package com.danielvaughan.rssreader.client.components; import com.extjs.gxt.ui.client.widget.ContentPanel; import com.extjs.gxt.ui.client.widget.layout.FitLayout; public class FeedPanel extends ContentPanel { public FeedPanel() { setHeading("Main"); setLayout(new FitLayout()); } }