您的位置:首页 > 其它

as3.0 puremvc sample

2013-01-02 09:51 197 查看
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:view="com.qiye.view.*" creationComplete="application1_creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import com.qiye.AppFacade;

import mx.events.FlexEvent;

protected function application1_creationCompleteHandler(event:FlexEvent):void
{
var myFacade:AppFacade = AppFacade.getInstance();
myFacade.startup(this);
}

]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<view:textView id="mytextView" x="50" y="50" />
</s:Application>


package com.qiye
{
import com.qiye.controller.RestartupCommand;
import com.qiye.controller.StartupCommand;

import org.puremvc.as3.interfaces.IFacade;
import org.puremvc.as3.patterns.facade.Facade;

public class AppFacade extends Facade implements IFacade
{

public static const START_UP:String = "start_up";
public static const REGISTER_UP:String = "register_up";

public function AppFacade()
{
super();
}

public static function getInstance():AppFacade
{
if(instance==null)
{
instance = new AppFacade;
}

return instance as AppFacade;
}

override protected function initializeController():void
{
super.initializeController();
registerCommand(START_UP,StartupCommand);
registerCommand(REGISTER_UP,RestartupCommand);
}

public function startup(app:StudyPureMVC):void
{
sendNotification(START_UP,app);
}
}
}


package com.qiye.controller
{
import com.qiye.AppFacade;
import com.qiye.model.TextProxy;
import com.qiye.view.TextMediator;

import org.puremvc.as3.interfaces.ICommand;
import org.puremvc.as3.interfaces.INotification;
import org.puremvc.as3.patterns.command.SimpleCommand;

public class StartupCommand extends SimpleCommand implements ICommand
{
public function StartupCommand()
{
super();
}

override public function execute(notification:INotification):void
{
var app:StudyPureMVC = notification.getBody() as StudyPureMVC;
facade.registerProxy(new TextProxy(TextProxy.NAME));
facade.registerMediator(new TextMediator(TextMediator.NAME,{mytextView:app.mytextView}));

sendNotification(AppFacade.REGISTER_UP,app);
}

}
}


package com.qiye.controller
{
import com.qiye.model.TextProxy;
import org.puremvc.as3.interfaces.ICommand;
import org.puremvc.as3.interfaces.INotification;
import org.puremvc.as3.patterns.command.SimpleCommand;

public class RestartupCommand extends SimpleCommand implements ICommand
{
public function RestartupCommand()
{
super();
}

override public function execute(notification:INotification):void
{
(facade.retrieveProxy(TextProxy.NAME) as TextProxy).getThis();
}

}
}


<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="100" height="50">
<fx:Script>
<![CDATA[

public static const UAPADATE:String = "updates";
[Bindable]
public var texts:String="";
[Bindable]
public var ttext:String ="sddsds";

]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:TextArea id="textArea" width="100%" height="50%" text="{texts}"/>
<s:TextInput x="0" y="25" width="40" text="@{ttext}"/>
<s:Button x="50" y="25" label="change" click="dispatchEvent(new Event(UAPADATE))" />
</s:Group>


package com.qiye.view
{

import com.qiye.model.TextProxy;

import flash.events.Event;

import mx.controls.Alert;

import org.puremvc.as3.interfaces.IMediator;
import org.puremvc.as3.interfaces.INotification;
import org.puremvc.as3.patterns.mediator.Mediator;

public class TextMediator extends Mediator implements IMediator
{

public static const NAME:String = "TextMediator";
//public static const CHANGE_EVENT:String = "changeevent";

public function TextMediator(mediatorName:String=null, viewComponent:Object=null)
{
super(mediatorName, viewComponent);
(viewComponent.mytextView as textView).addEventListener(textView.UAPADATE,update_handler);
}

protected function update_handler(e:Event):void
{
//Alert.show("fsdfs");
//facade.registerCommand(CHANGE_EVENT,ChangeValueCommand);
//sendNotification(CHANGE_EVENT);
(viewComponent.mytextView as textView).texts =(viewComponent.mytextView as textView).ttext;
//Alert.show("1");
}

override public function listNotificationInterests():Array
{
return [ TextProxy.DATE_OVER ];
}

override public function handleNotification(notification:INotification):void
{
switch(notification.getName())
{
case  TextProxy.DATE_OVER:
(viewComponent.mytextView as textView).texts = notification.getBody().values as String;
break;
}
}

}
}


package com.qiye.model
{
import com.qiye.model.vo.TextVo;

import org.puremvc.as3.interfaces.IProxy;
import org.puremvc.as3.patterns.proxy.Proxy;

public class TextProxy extends Proxy implements IProxy
{
public static const NAME:String ="TextProxy";

public static const DATE_OVER:String = "date_over";

public function TextProxy(proxyName:String=null, data:Object=null)
{
super(proxyName, data);
}

public function getThis():void
{
var tvo:TextVo = new TextVo();
data = new Object;
data.values= tvo.vos;
sendNotification(DATE_OVER,data);
}
}
}


package com.qiye.model.vo
{
public class TextVo
{

public var vos:String = "hello world !";

public function TextVo()
{
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: