Cairngorm.docx
《Cairngorm.docx》由会员分享,可在线阅读,更多相关《Cairngorm.docx(21页珍藏版)》请在冰豆网上搜索。
Cairngorm
Cairngorm
Cairngorm是Abode推出的FlexRIAs框架,其实是MVC的发扬光大,它由七大部分组成。
1.Model(数据模型M):
使用singleton(单例)模式共享变量;
2.VO(ValueObject):
储存特定值对象变量。
3.VIew(视图V):
将Model的数据绑定互组件并广播侦听CairngormEvent;
4.Controller(控制器):
侦听Cairngorm事件并将其映射到CairngormCommand;
5.Command(命令):
调用CairngormDelegate或其它Command,并更新Model;
6.Delegate(委托):
实例化RPC(远程过程调用)并将其结果返回给Command;
7.Business(业务):
定义RPC。
使用Cairngorm的第一步是建立框架结构的骨架,包括了三个对象:
ModelLocater;
ServiceLocator;
FrontController;
ModelLocator:
承载了组件之间的所有的传递的信息和数据,这是一个Bindable(可绑定的)对象。
ServiceLocator:
定义了与数据源(Httpservice,Webservice,Remoteobject)之间通讯的界面。
FrontController:
建立播送事件(Dispatchevent)和命令层(command)之间的对应关系(mapping)。
看一下相关的代码:
BuddyAppModelLocator.as:
Xml代码
1.package com.ny.flex.cairngorm.model
2.{
3. import com.ny.flex.cairngorm.vo.User;
4.
5. import mx.collections.ArrayCollection;
6.
7. [Bindable]
8. public class BuddyAppModelLocator
9. {
10. public var buddyList:
ArrayCollection=new ArrayCollection();
11. public var loginUser:
User=new User();
12. public var viewStackSelectedIndex :
int = 0;
13.
14. static private var __instance:
BuddyAppModelLocator=null;
15.
16. static public function getInstance():
BuddyAppModelLocator
17. {
18. if(__instance == null)
19. {
20. __instance=new BuddyAppModelLocator();
21. }
22. return __instance;
23. }
24. }
25.}
26.
packagecom.ny.flex.cairngorm.model
{
importcom.ny.flex.cairngorm.vo.User;
importmx.collections.ArrayCollection;
[Bindable]
publicclassBuddyAppModelLocator
{
publicvarbuddyList:
ArrayCollection=newArrayCollection();
publicvarloginUser:
User=newUser();
publicvarviewStackSelectedIndex:
int=0;
staticprivatevar__instance:
BuddyAppModelLocator=null;
staticpublicfunctiongetInstance():
BuddyAppModelLocator
{
if(__instance==null)
{
__instance=newBuddyAppModelLocator();
}
return__instance;
}
}
}
在ModelLocator代码中,定义了三个public的变量,buddyList:
用来存放由数据库获取的密友列表;loginUser:
定义一个User类型对象;viewStackSelectedIndex:
定义viewStack指向的视窗。
几乎所有的服务层返回的信息都需要在ModelLocator中有一个相应的对象。
BuddyServiceLocator.mxml:
Xml代码
1.
xml version=”1.0″ encoding=”utf-8″?
>
2.ServiceLocator xmlns:
mx=” xmlns:
cairngorm=”
3. RemoteObject id=”buddyRo“ destination=”flexmvcRO” >
4.
5.
RemoteObject>
6.
ServiceLocator>
7.
xmlversion=”1.0″encoding=”utf-8″?
>
ServiceLocatorxmlns:
mx=”xmlns:
cairngorm=”
RemoteObjectid=”buddyRo“destination=”flexmvcRO”>
RemoteObject>
ServiceLocator>
上述代码定义了程序将要调用的RemoteObject ,RemoteObject 所调用的Destination需要和remote_config.xml文件中的Destination相一致。
在此,Destination的值为“flexmvcRO”。
BuddyListController.as:
Xml代码
1.package com.ny.flex.cairngorm.control
2.{
3. import com.adobe.cairngorm.control.FrontController;
4. import mand.GetBuddyListCommand;
5. import mand.LoginCommand;
6. import com.ny.flex.cairngorm.event.GetBuddyListEvent;
7. import com.ny.flex.cairngorm.event.LoginEvent;
8.
9. public class BuddyListController extends FrontController
10. {
11. public function BuddyListController()
12. {
13. super();
14. addCommand(LoginEvent.LOGIN_EVENT,LoginCommand);
15. addCommand(GetBuddyListEvent.GET_BUDDY_LIST_EVENT,
16.GetBuddyListCommand);
17. }
18.
19. }
20.}
21.
packagecom.ny.flex.cairngorm.control
{
importcom.adobe.cairngorm.control.FrontController;
importmand.GetBuddyListCommand;
importmand.LoginCommand;
importcom.ny.flex.cairngorm.event.GetBuddyListEvent;
importcom.ny.flex.cairngorm.event.LoginEvent;
publicclassBuddyListControllerextendsFrontController
{
publicfunctionBuddyListController()
{
super();
addCommand(LoginEvent.LOGIN_EVENT,LoginCommand);
addCommand(GetBuddyListEvent.GET_BUDDY_LIST_EVENT,
GetBuddyListCommand);
}
}
}
很显然,上述的Controller代码是事件和命令的对应处理的地方。
如何能将这些乱七八糟的东西结合在一起?
其Magic的地方是在主页(Mainapplication)上,代码如下:
BuddList_Main_Cairngorm.mxml:
Xml代码
1.
xml version=”1.0″ encoding=”utf-8″?
>
2.Application xmlns:
mx=” xmlns:
service=”com.ny.flex.cairngorm.service.*“ xmlns:
controller=”com.ny.flex.cairngorm.control.*” xmlns:
views=”com.ny.flex.cairngorm.views.*” layout=”absolute“ width=”100%” height=”100%“>
3.Script>
4.
[CDATA[
5. import com.ny.flex.cairngorm.model.BuddyAppModelLocator;
6. [Bindable]
7. public var myModel:
BuddyAppModelLocator = BuddyAppModelLocator.getInstance();
8. ]]>
9.
Script>
10.
11. BuddyServiceLocator id=”myservice“/>
12. BuddyListController id=”myController“/>
13.
14. HBox horizontalAlign=”center” verticalAlign=”top“ width=”100%” height=”100%” y=”0” x=”0“>
15. ViewStack id=”viewStack“ resizeToContent=”true” selectedIndex=”{myModel.viewStackSelectedIndex}” >
16. LoginView />
17. BuddyListView/>
18.
ViewStack>
19.
HBox>
20.
Application>
21.
xmlversion=”1.0″encoding=”utf-8″?
>
Applicationxmlns:
mx=”xmlns:
service=”com.ny.flex.cairngorm.service.*“xmlns:
controller=”com.ny.flex.cairngorm.control.*”xmlns:
views=”com.ny.flex.cairngorm.views.*”layout=”absolute“width=”100%”height=”100%“>
Script>
[CDATA[
importcom.ny.flex.cairngorm.model.BuddyAppModelLocator;
[Bindable]
publicvarmyModel:
BuddyAppModelLocator=BuddyAppModelLocator.getInstance();
]]>
Script>
BuddyServiceLocatorid=”myservice“/>
BuddyListControllerid=”myController“/>
HBoxhorizontalAlign=”center”verticalAlign=”top“width=”100%”height=”100%”y=”0”x=”0“>
ViewStackid=”viewStack“resizeToContent=”true”selectedIndex=”{myModel.viewStackSelectedIndex}”>
LoginView/>
BuddyListView/>
ViewStack>
HBox>
Application>
现在用户可以建立视图组件,并从这些组件中播送事件:
LoginView.mxml:
Xml代码
1. [CDATA[
2. import com.ny.flex.cairngorm.event.LoginEvent;
3. import com.ny.flex.cairngorm.vo.User;
4. import mx.validators.Validator;
5. private function login():
void{
6. if(Validator.validateAll(validators).length == 0){
7. var loginUser:
User = new User();
8. loginUser.userName=username.text;
9. loginUser.password=password.text;
10. var loginEvent:
LoginEvent = new LoginEvent();
11. loginEvent.loginUser = loginUser;
12. loginEvent.dispatch();
13. }
14. }
15. ]]>
16.
Script>
17.
18.
– Validators–>
19. Array id=”validators“>
20. StringValidator id=”userNameValidator” source=”{username}“ property=”text“ required=”true“/>
21. StringValidator id=”passwordValidator” source=”{password}“ property=”text” required=”true” />
22.
Array>
23.
24.
25.Form id=”loginForm” x=”0” y=”0“>
26. FormItem label=”Username:
” >
27. TextInput id=”username” />
28.
FormItem>
29. FormItem label=”Password:
” >
30. TextInput id=”password” displayAsPassword=”true” />
31.
FormItem>
32. FormItem direction=”horizontal” verticalGap=”15” paddingTop=”5” width=”170“>
33. Button id=”loginBtn” label=”Login” click=”login()”/>
34.
FormItem>
35.
Form>
36.
37.
Panel>
38.
[CDATA[
importcom.ny.flex.cairngorm.event.LoginEvent;
importcom.ny.flex.cairngorm.vo.User;
importmx.validators.Validator;
privatefunctionlogin():
void{
if(Validator.validateAll(validators).length==0){
varloginUser:
User=newUser();
loginUser.userName=username.text;
loginUser.password=password.text;
varloginEvent:
LoginEvent=newLoginEvent();
loginEvent.loginUser=loginUser;
loginEvent.dispatch();
}
}
]]>
Script>
–Validators–>
Arrayid=”validators“>
StringValidatorid=”userNameValidator”source=”{username}“property=”text“required=”true“/>
StringValidatorid=”passwordValidator”source=”{password}“property=”text”required=”true”/>
Array>
Formid=”loginForm”x=”0”y=”0“>
FormItemlabel=”Username:
”>
TextInputid=”username”/>
FormItem>
FormItemlabel=”Password:
”>
TextInputid=”password”displayAsPassword=”true”/>
FormItem>
FormItemdirection=”horizontal”verticalGap=”15”paddingTop=”5”width=”170“>
Buttonid=”loginBtn”label=”Login”click=”login()”/>
FormItem>
Form>
Panel>
每一个动作都需要建立一个相应的事件:
LoginEvent.as:
Xml代码
1.package com.ny.flex.cairngorm.event
2.{
3. import com.adobe.cairngorm.c