FLEX中简单的 service 层与VIEW层分离的方法
一、service层
service类 继承 mx.rpc.http.mxml.HTTPService
在这个service类中实现数据请求,以及简单的逻辑处理
代码:
package services {
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.mxml.HTTPService;
import valueObjects.Product;
public class ProductService extends HTTPService {
[Bindable]
public var products:ArrayCollection;
public function ProductService(rootURL:String=null, destination:String=null) {
super(rootURL, destination);
this.resultFormat="e4x";
this.url="http://www.flexgrocer.com/categorizedProducts.xml";
addEventListener(ResultEvent.RESULT, handleProductResult);
}
private function handleProductResult( event:ResultEvent ):void {
var productsArray:Array = new Array();
var resultData:XMLList = event.result..product;
for each (var p:XML in resultData) {
var product:Product = Product.buildProductFromAttributes( p );
productsArray.push( product );
}
products = new ArrayCollection( productsArray );
}
}
}
二、View层代码:
在View层直接用service层的标签
代码:
<?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"
xmlns:views="views.*" xmlns:services="services.*"
creationComplete="handleCreationComplete(event)">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<services:CategoryService id="categoryService"/>
<services:ProductService id="productService"/>
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
private function handleViewCartClick( event:MouseEvent ):void {
bodyGroup.currentState="cartView";
}
private function handleCreationComplete(event:FlexEvent):void {
categoryService.send();
productService.send();
}
]]>
</fx:Script>
<s:controlBarLayout>
<s:BasicLayout/>
</s:controlBarLayout>
<s:controlBarContent>
<s:Button y="10" label="Checkout" id="btnCheckout" right="10"/>
<s:Button y="10" label="View Cart" id="btnCartView" right="90" click="handleViewCartClick( event )"/>
<s:Button label="Flex Grocer" x="5" y="5"/>
<s:List left="200" height="40" dataProvider="{categoryService.categories}" labelField="name">
<s:layout>
<s:HorizontalLayout/>
</s:layout>
</s:List>
</s:controlBarContent>
<s:Label text="(c) 2009, FlexGrocer" right="10" bottom="10"/>
<views:ShoppingView id="bodyGroup"
width="100%" height="100%"
groceryInventory="{productService.products}"/>
</s:Application>
分享到:
相关推荐
FLEX 实现页面与ActionScript代码完全分离(后缀名丢失,下载后自行添加.rar)
flex 简易计算器flex 简易计算器flex 简易计算器flex 简易计算器
FlexService2FlexService2FlexService2FlexService2FlexService2FlexService2FlexService2FlexService2FlexService2FlexService2FlexService2FlexService2FlexService2FlexService2
flex中文帮助flex中文帮助flex中文帮助flex中文帮助
flex和多种语言的整合,flex和webservice的整合
flex call web Service
flex中文帮助文档flex中文帮助文档
tomcat中部署安装flex service的安装方法
Arcgis flexView 说明文档。
Flex中加载SWF方法,介绍了flex 对 swf 加载和处理
因此会导致在编辑时找代码位置很麻烦,然后我就开始寻找让view和代码分离的方法,本来知道一些flex开发框架cairngorm,puremvc等等,cairngorm太烦琐,puremvc好很多,但是我的目的仅仅只是把函数代码从mxml中分离而已,...
Flex万年历记事本_flex源码
MXML语法.MXML flexview actionscript
FLEX中文教程+实例
flex 中文帮助文档flex 中文帮助文档flex 中文帮助文档flex 中文帮助文档flex 中文帮助文档flex 中文帮助文档flex 中文帮助文档flex 中文帮助文档flex 中文帮助文档flex 中文帮助文档flex 中文帮助文档flex 中文帮助...
Flex技术中AdvancedDataGrid使用方法
Flex中文帮助 学习Flex的好资料 入门提高都实用
Flex_Data_Service基础教程
Flex学习文档,Flex中文文档,,记载了各种学习心得