GXT之旅:第七章:MVC——MVC重构项目(3)

使用MVC重构RSSReader项目——FeedPanel区域

下面,按照同样的逻辑,去处理FeedPanel区域。

  • 在AppEvents类,定义一个新的EventType——FeedPanelReady

public static final EventType FeedPanelReady = new EventType();

  • 在package:com.danielvaughan.rssreader.client.mvc.controllers,新建类FeedController extends Controller

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,新建FeedView extends View

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) {
	}

}

  • 在FeedController的构造函数里,注册Init EventType

public FeedController() {
registerEventTypes(AppEvents.Init);
}

  • 在FeedController类里,定义FeedView属性,在initialize方法了,实例化他。并且实现handleEvent方法,将接收的event转发到feedview里。

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);
	}

}

  • 重命名RssMainPanel为FeedPanel。在FeedView里加入FeedPanel的实例属性

private final FeedPanel feedPanel = new FeedPanel();

  • 在FeedView类里,加入onInit的对应事件处理方法。其onInit方法的内容是派发另一个事件FeedPanelReady。并且处理handleEvent方法,根据Init事件类型,调用onInti方法

    @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));
    }

  • 在AppControllers类里,注册FeedPanelReady事件

	public AppController() {
		registerEventTypes(AppEvents.Init);
		registerEventTypes(AppEvents.Error);
		registerEventTypes(AppEvents.UIReady);
		registerEventTypes(AppEvents.NavPanelReady);
		registerEventTypes(AppEvents.FeedPanelReady);
	}

  • 在AppView类里,编写处理FeedPanelReady事件的对应方法——onFeedPanelReady。并且修改AppView的handleEvent方法

	@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);
	}

  • 最后,在RSSReader的onModule,加入FeedController控制器

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);
	}
}


  • 最后执行效果如下



  • FeedPanel类的完整内容如下(取出之前的例子代码):

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());
	}
}




你可能感兴趣的:(mvc,service,Class)