package event { import com.adobe.cairngorm.control.CairngormEvent; public class MyEvent extends CairngormEvent { public static const ACTION:String="ACTION"; public var message:String; public function MyEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false) { trace("event"); super(type, bubbles, cancelable); } } }
我没用service跟 delegates 他两好像只是为了与后台服务了,大概意思就是
控制myControl注册了command(注册了监听器),
view层当你发出事件时,交给 command 去处理,
command会改变Model 里边的数据
(因为Model与view绑定在一起,所以改变了model里边的数据,
package commad { import com.adobe.cairngorm.commands.ICommand; import com.adobe.cairngorm.control.CairngormEvent; import event.MyEvent; import model.MyModel; public class Mycommand implements ICommand { public function Mycommand() { } public function execute(e:CairngormEvent):void { if(e is MyEvent){ var ep:MyEvent=e as MyEvent; var myModelLocator:MyModel=MyModel.getInstance(); myModelLocator.products=ep.message; }else{ throw new Error; } } } }
view层数据也改变了)
package model { import com.adobe.cairngorm.model.IModelLocator; import com.adobe.cairngorm.*; [Bindable] public class MyModel implements IModelLocator { private static var instance : MyModel public var products : String public function MyModel() { if ( instance != null ) { trace("error"); throw new CairngormError( CairngormMessageCodes.SINGLETON_EXCEPTION, "MyModel" ); } instance = this; } public static function getInstance() : MyModel { trace("model"); if ( instance == null ) instance = new MyModel(); return instance; } } }
package MyControl { import com.adobe.cairngorm.control.FrontController; import event.MyEvent; import commad.Mycommand; public class MyControl extends FrontController { public function MyControl() { trace("control constractor is called"); super(); addCommand(MyEvent.ACTION,Mycommand); } } }
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:MyControl="MyControl.*"> <MyControl:MyControl> </MyControl:MyControl> <mx:VBox> <mx:Button click="dispather()"> </mx:Button> <mx:Script> <![CDATA[ import model.MyModel; import event.MyEvent; import com.adobe.cairngorm.control.CairngormEventDispatcher; private var i:int; public function dispather():void{ // new MyModel(); var myevent:MyEvent=new MyEvent(MyEvent.ACTION); myevent.message=i+""; myevent.dispatch(); //两个方法都可以 // CairngormEventDispatcher.getInstance().dispatchEvent(myevent); i++; } ]]> </mx:Script> <mx:Text text="{model.MyModel.getInstance().products}"> </mx:Text> </mx:VBox> </mx:Application>