Adobe Flex 3 , 简写flex3,是跨平台的互联网富应用rich Internet applications (RIAs)框架,flex可以使你创建出漂亮的,高效的跨越主流浏览器和操作系统的应用。(摘自 里克的地下室 )
flex和rails的结合将是一种性感的结合,那么本人从今天开始研究flex on rails
第一篇 基础 (Flex3 SDK)
开发工具准备:
Download Adobe Flex Builder 3.0.2 Professional 比较强大,你也可以只下载 Flex 3 SDK
安装略
开始第一个Rails和flex结合的例子:(先使用Flex3 SDK)
①、创建rails app
>>rails blogs -d mysql
>>cd blogs
>>ruby script/generate scaffold post title:string body:text
>>rake db:migrate
②、创建flex的mxml文件
在app目录下,新建文件夹flex,在flex下新建文件Posts.mxml 内容:如下
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" applicationComplete="index.send()" layout="vertical">
<mx:HTTPService resultFormat="e4x" url="http://localhost:3000/posts.xml" id="index"></mx:HTTPService>
<mx:DataGrid dataProvider="{index.lastResult.post}" height="100%" width="100%">
<mx:columns>
<mx:DataGridColumn headerText="Title" dataField="title"></mx:DataGridColumn>
<mx:DataGridColumn headerText="Body" dataField="body"></mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
</mx:Application>
③通过flex3 SDK将Posts.mxml 转换为Posts.swf, 使用SDK目录下的bin/mxmlc.exe(将其添加到环境变量中)
>>cd app
>>mxmlc -output=public/bin/Posts.swf app/flex/Posts.mxml
成功后,在app/public/bin目录下就有Posts.swf文件
④、浏览器打开Posts.swf文件: http://localhost:3000/bin/Posts.swf
结果如图:
OK,今天就到这里^_^
补充:如果查看 http://localhost:3000/bin/Posts.swf
出现以下错误:
[RPC Fault faultString="Security error accessing url" faultCode="Channel.Security.Error" faultDetail="Destination: DefaultHTTP" ]
at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::faultHandler()
at mx.rpc::Responder/fault()
at mx.rpc::AsyncRequest/fault()
at DirectHTTPMessageResponder/securityErrorHandler()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/redirectEvent()
那是因为 Flex 不使用代理时遇到安全沙箱冲突 。
解决方案:在../public目录下新建一个文件:crossdomain.xml
内容为:
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*" />
<allow-http-request-headers-from domain="*" headers="*"/>
</cross-domain-policy>
就OK了 ^_^
参考资料:
http://www.elctech.com/tutorials/flex-on-rails
http://hi.baidu.com/%D0%C7203/blog/item/70e4c918dc468cb74bedbc5a.html
https://www.salesforce.com/services/crossdomain.xml
http://www.riafan.com/article.asp?id=102