最近在学Robotlegs,在百度和谷歌里搜了几天,也没找到基本有价值的书(文章、demo...),更别说是纯AS3的。没办法,自己动手,丰衣足食吧(毕竟学的不精,若有错误,望斧正!)
1.先建立一个新的AS3Project
2.将Robotlegs的robotlegs-framework-v1.5.2.swc、SwiftSuspenders-v1.6.0.swc放到lib下,导入到库
3.在Main里new一个HelloWorldContext
package { import flash.display.Sprite; import flash.events.Event; import HelloWorld.HelloWorldContext; /** * ... * @author Fancy */ public class Main extends Sprite { public function Main():void { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); var context:HelloWorldContext = new HelloWorldContext(this); } } }
4.在HelloWorldContext里继承Context
package HelloWorld { import flash.display.DisplayObjectContainer; import org.robotlegs.mvcs.Context; /** * ... * @author Fancy */ public class HelloWorldContext extends Context { public function HelloWorldContext(contextView:DisplayObjectContainer,autoStartup:Boolean = true) { super(contextViewm,autoStartup); } } }
5.在HelloWorldContext里改写startup方法,并添加model
override public function startup():void { injector.mapSingleton(HelloWorldModel); }
6.添加事件处理类HelloEvent
package HelloWorld.events { import flash.events.Event; /** * ... * @author Fancy */ public class HelloEvent extends Event { public static const REQUEST_SHOW:String = "request_show"; public static const RESPONSE_SHOW:String = "response_show"; public var data:Object; public function HelloEvent(type:String) { super(type); } } }
7.设置数据类HelloWorldModel
package HelloWorld.models { import HelloWorld.events.HelloEvent; import org.robotlegs.mvcs.Actor; /** * ... * @author Fancy */ public class HelloWorldModel extends Actor { private var showStr:String; public function HelloWorldModel() { showStr = "Hello World!"; } //派发响应方法 public function requestStr():void { var eve:HelloEvent = new HelloEvent(HelloEvent.RESPONSE_SHOW); eve.data = { "name":showStr }; dispatch(eve); } } }
8.添加View -- 显示文本框
package HelloWorld.views { import flash.display.Sprite; import flash.text.TextField; /** * ... * @author Fancy */ public class HelloWorldView extends Sprite { private var tf_word:TextField; public function HelloWorldView() { tf_word = new TextField(); tf_word.x = 100; tf_word.y = 100; addChild(tf_word); } } }
9.处理HelloWorldView的Mediator,注入HelloWorldView,并改写onRegister
package HelloWorld.views { import org.robotlegs.mvcs.Mediator; /** * ... * @author Fancy */ public class HelloWorldMediator extends Mediator { [Inject] public var helloView:HelloWorldView; override public function onRegister():void { } } }
10.派发Mediator的请求事件
override public function onRegister():void { dispatch(new HelloEvent(HelloEvent.REQUEST_SHOW)); }
11.在Context中添加view映射和事件映射
override public function startup():void { mediatorMap.mapView(HelloWorldView,HelloWorldMediator); injector.mapSingleton(HelloWorldModel); }
12.创建处理Command
package HelloWorld.controller { import HelloWorld.models.HelloWorldModel; import org.robotlegs.mvcs.Command; /** * ... * @author Fancy */ public class HelloWorldCommand extends Command { [Inject] public var helloModel:HelloWorldModel; override public function execute():void { helloModel.requestStr(); } } }
13.这时Model已经派发了HelloEvent.RESPONSE_SHOW事件,当然要去Mediator里接收了
override public function onRegister():void { eventMap.mapListener(eventDispatcher,HelloEvent.RESPONSE_SHOW,onShowHandle); dispatch(new HelloEvent(HelloEvent.REQUEST_SHOW)); } private function onShowHandle(e:HelloEvent):void { helloView.updateWord(e.data); }
14.在view中添加updateWord()
public function updateWord(data:Object):void { tf_word.text = data["name"]; }
15.最后我们在new 一个view到context里,就成功了
override public function startup():void { mediatorMap.mapView(HelloView, HelloViewMediator); injector.mapSingleton(HelloModel); commandMap.mapEvent(HelloEvent.REQUEST_SHOW, HelloCommand); contextView.addChild(new HelloView()); }
转自:天地会http://bbs.9ria.com/thread-178881-1-1.html
http://bbs.9ria.com/forum.php?mod=viewthread&tid=218294&highlight=Robotlegs
时间: 2024-11-07 02:25:53