天天看點

ActionScript 3.0中得RobotLegs應用

ActionScript 3.0中得RobotLegs應用

    • 簡介
    • Context
    • Mediator
    • Model
    • 注意事項

簡介

MVC在大多數眼中是一種設計模式,有經驗得碼農完全可以按照自己對MVC得了解将MVC思想應用到實際項目中而不依賴任何MVC架構。

大名鼎鼎的PurMVC是我日常項目中使用最多的,PurMVC強調分離,适用性很強,但是比較龐大,在開發一些小玩意兒得時候總是感覺殺雞在用宰牛刀。

還有一些如

Cairngorm:限制太多,完全按照設計好的流程走,隻能用它得model去更新對應的view,在實際項目應用中比較死闆,涉及到多view關聯會比較混亂;

Mate:更像為Flex定制的,用Flex就很簡便;

RobotLegs:采用和Spring類似的注入方式,這裡叫做依賴注入(Dependency Injection),好處是比pureMVC更友善,是以一些人會認為Robotlegs比pureMVC好用

初期得Robotlegs是專為as3而設計的ActionScript 3 robotlegs-framework,現在同樣适用于C#項目C# robotlegs-sharp-framework中。

ActionScript 3.0中得RobotLegs應用

Context

Context 是學習 Robotlegs 的核心,一個Context相當于一個框,具體框裡裝啥内容你自己決定,但是接口使用方法是固定得,一般大型項目多個Moudle得情況下需要多個Context。

CommandMap 類的依賴注入

為了執行随後被映射得Command,需要先給commandMap提供一個可執行得類和事件類型

// Map some Commands to Events
commandMap.mapEvent(ContextEvent.STARTUP_COMPLETE, CreateBallCommand, ContextEvent, true);
commandMap.mapEvent(HelloFlashEvent.BALL_CLICKED, CreateBallCommand, HelloFlashEvent );
           

MediatorMap 類的依賴注入

mediators 映射到 view 友善後續得注入,這也是RobotLegs最友善得地方

mediatorMap.mapView(Ball, BallMediator);
mediatorMap.mapView(Readout, ReadoutMediator);
           

Mediator

作為中介,接收發送view得事件,并且改變view得顯示,我接觸得架構得Mediator所需要做得工作似乎都大同小異。

package org.robotlegs.demos.helloflash.view
{
	import flash.events.MouseEvent;
	
	import org.robotlegs.demos.helloflash.controller.HelloFlashEvent;
	import org.robotlegs.demos.helloflash.model.StatsModel;
	import org.robotlegs.mvcs.Mediator;
	
	public class BallMediator extends Mediator
	{
		[Inject]
		public var view:Ball;
		
		[Inject]
		public var statsModel:StatsModel;
		
		public function BallMediator()
		{
			// Avoid doing work in your constructors!
			// Mediators are only ready to be used when onRegister gets called
		}
		
		override public function onRegister():void
		{
			// Listen to the view
			eventMap.mapListener(view, MouseEvent.CLICK, onClick);
			// Listen to the context
			eventMap.mapListener(eventDispatcher, HelloFlashEvent.BALL_CLICKED, onSomeBallClicked);
		}
		
		protected function onClick(e:MouseEvent):void
		{
			// Manipulate the model
			statsModel.recordBallClick();
			// Dispatch to context
			eventDispatcher.dispatchEvent(new HelloFlashEvent(HelloFlashEvent.BALL_CLICKED));
		}
		
		protected function onSomeBallClicked(e:HelloFlashEvent):void
		{
			// Manipulate the view
			view.poke();
		}
	}
}
           

Model

封裝資料,并提供資料通路,自不必說,Model你就是幹這個的。

package org.robotlegs.demos.helloflash.model
{
	import org.robotlegs.mvcs.Actor;
	
	public class StatsModel extends Actor
	{
		protected var _ballClickCount:int;
		
		public function StatsModel()
		{
			_ballClickCount = 0;
		}
		
		public function recordBallClick():void
		{
			_ballClickCount++;
		}
		
		public function get ballClickCount():int
		{
			return _ballClickCount;
		}
	}
}
           

注意事項

架構使用很簡單,主要就是正确使用Injectors就可以了,綁定正确得Mediator到對應得View,如果映射沒有觸發,那就去查command。

RobotLegs的Demo原本是一個web項目,由于大多浏覽器目前已不在支援直接通路本地flash,為了友善,這裡改成了桌面的air項目robotLegs-as3,如果遇到AIR SDK問題可參考Flash Builder 4.6桌面項目在Flash Builder 4.7中打開遇到的問題。

繼續閱讀