Adobe Flex – Amfphp Service Class
Here is a very helpful script for creating a connection between your Flex/Flash application and amfphp.
We are mimicking the available methods that are on our server. You could easily add methods to both ends to create more functionality.
Code is used as follows:
1: package com.jonniespratley.snippr.services
2: {
3: import com.jonniespratley.snippr.model.ModelLocator;
4: import com.jonniespratley.snippr.vo.SnippetVO;
5:
6: import flash.net.NetConnection;
7: import flash.net.Responder;
8: import flash.utils.ByteArray;
9:
10: import mx.collections.ArrayCollection;
11: import mx.controls.Alert;
12: import mx.controls.Image;
13: import mx.controls.Text;
14: import mx.core.Window;
15: import mx.rpc.events.ResultEvent;
16:
17: /**
18: * This file is for use without! using the services-config.xml file
19: * @author Jonnie
20: *
21: */
22: public class SnipprService
23: {
24: private static var _service:NetConnection;
25: private var model:ModelLocator = ModelLocator.getInstance();
26:
27: //Here we are creating a new connection to our amfphp service, when this is instantiated, it connects to our service
28: public function SnipprService()
29: {
30: _service = new NetConnection();
31: _service.connect( "http://localhost/snippr/amfphp/gateway.php" );
32: }
33:
34:
35: /* ***************************************
36: *** Service Calls
37: *****************************************/
38:
39: //Here we are calling the getSnippets on our server (amfphp) and setting the result and fault handlers
40: public function getSnippets():void
41: {
42: _service.call( "snippr.SnipprService.getSnippets", new Responder( snippetResultHandler, snipprFaultHandler ) );
43: }
44:
45: //We take one argument here, and that is a snippet, because our server (amfphp) is expecting a snippetVO
46: public function saveSnippet( snippet:SnippetVO ):void
47: {
48: _service.call( "snippr.SnipprService.saveSnippet", new Responder( snippetSavedHandler, snipprFaultHandler ), snippet );
49: }
50:
51: //We take one argument here, and that is the id of the snippet we are wanting to remove
52: public function removeSnippet( snippet_id:uint ):void
53: {
54: _service.call( "snippr.SnipprService.removeSnippet", new Responder( snippetRemoveHandler, snipprFaultHandler ), snippet_id );
55: }
56:
57: //We take two arguments here, one is a byte array and the other is the filename of the file
58: public function takeSnapshot( bytes:ByteArray, filename:String ):void
59: {
60: _service.call( "snippr.SnipprService.takeSnapshot", new Responder( snapshotResultHandler, snipprFaultHandler ), bytes, filename );
61: }
62:
63: /* ***************************************
64: *** Result and Fault Handlers
65: *****************************************/
66:
67: //Here we are handling the result coming back as an array of snippets, then we add our snippets to our model
68: private function snippetResultHandler( data:Array ):void
69: {
70: model.snippetCollection = initVO( data );
71: }
72:
73: private function snapshotResultHandler( data:Object ):void
74: {
75: var result:ResultEvent = data as ResultEvent;
76:
77: /* This is just for some fun */
78: var w:Window = new Window();
79: w.width = 600;
80: w.height = 500;
81: w.layout = "vertical";
82:
83: var i:Image = new Image();
84: i.source = data.result;
85:
86: var t:Text = new Text();
87: t.text = "Nice shot, and nice app, Here is the link for the screenshot.n " + data.result;
88:
89: w.addChild( i );
90: w.addChild( t );
91: w.open();
92:
93: trace( data.result );
94: }
95:
96: //Helper for the result
97: private function initVO( resultArray:Array ):ArrayCollection
98: {
99: var tempArray:ArrayCollection = new ArrayCollection();
100:
101: for ( var s:String in resultArray )
102: {
103: tempArray.addItem( new SnippetVO( resultArray[s] ) );
104:
105: }
106: return tempArray;
107: }
108:
109: //Here we are handling the result and adding it to the value of serviceResponse in our model
110: private function snippetSavedHandler( data:Object ):void
111: {
112: ModelLocator.getInstance().serviceResponse = data.toString();
113: }
114:
115: /*
116: Here we are handling the result that is being returned, which will be the id of the removed snippet, and
117: removing it from our model, at the snippet index
118: */
119: private function snippetRemoveHandler( data:Object ):void
120: {
121: getSnippets();
122: }
123:
124: //Here we are alerting the user that there was an error connection to our server
125: private function snipprFaultHandler( fault:Object ):void
126: {
127:
128: Alert.show( "There was an error connecting to the server.", "Snippr Service Error" );
129: }
130: }
131: }
Here is the value object for this class
SnippetVO.as:
1: package com.jonniespratley.snippr.vo
2: {
3: [RemoteClass(alias="vo.SnippetVO")]
4:
5: /**
6: * VOs are used to create a layer of business objects that can be
7: * transferred between tiers, instead of using records, results sets, and datasets.
8: */
9: [Bindable]
10: public class SnippetVO
11: {
12: public var snippet_id:int;
13: public var snippet_title:String;
14: public var snippet_code:String;
15: public var snippet_type:String;
16: public var snippet_created:String;
17: public var snippet_user:String;
18:
19: /**
20: * Helper function for building the data.
21: * @param source
22: *
23: */
24: public function SnippetVO( source:Object = null )
25: {
26: if ( source != null )
27: {
28: for ( var item:String in source )
29: {
30: try
31: {
32: this[item] = source[item];
33: }
34: catch ( error:Error )
35: {
36: throw new Error( "SnippetVO: " + error );
37: }
38: }
39: }
40: }
41: }
42: }
Here is also examples of using this class in a view.
1: <?xml version="1.0" encoding="utf-8"?>
2: <!--SnippetList-->
3: <mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="200" height="100%" creationComplete="init()">
4:
5: <mx:Script>
6: <![CDATA[
7: import mx.controls.Alert;
8: import mx.events.CloseEvent;
9: import com.jonniespratley.snippr.services.SnipprService;
10: import com.jonniespratley.snippr.vo.SnippetVO;
11: import com.jonniespratley.snippr.events.SnippetGetEvent;
12: import com.jonniespratley.snippr.model.ModelLocator;
13:
14: //Make a instance of our model for our data display
15: [Bindable] private var model:ModelLocator = ModelLocator.getInstance();
16:
17: //Make a variable to check weither there is a selected snippet or not
18: [Bindable] private var isSelected:Boolean = false;
19:
20: //Make variable of our service
21: private var service:SnipprService;
22:
23:
24: private function init():void
25: {
26: service = new SnipprService();
27: getSnippets();
28: }
29:
30: //Send a call to get the snippets
31: private function getSnippets():void
32: {
33: service.getSnippets();
34: }
35:
36: private function removeSnippet():void
37: {
38: Alert.show( "Are you sure?", "Remove Snippet", 3, null, removeSnippetAlertHandler );
39: }
40:
41: private function removeSnippetAlertHandler( event:CloseEvent ):void
42: {
43: if ( event.detail == Alert.YES )
44: {
45: service.removeSnippet( lt_snippets.selectedItem.snippet_id );
46: }
47: }
48:
49: //Make sure we handle the selected snippet and bind it to our model
50: private function selectHandler( event:Event ):void
51: {
52: isSelected = true;
53: model.selectedSnippet = event.target.selectedItem as SnippetVO;
54: }
55:
56: ]]>
57: </mx:Script>
58:
59: <!--Helper for Data Binding-->
60: <mx:Binding destination="lt_snippets.selectedItem" source="model.selectedSnippet"/>
61:
62: <!--List of Snippets-->
63: <mx:List id="lt_snippets"
64: dataProvider="{ model.snippetCollection }"
65: change="selectHandler( event )"
66: labelField="snippet_title"
67: width="100%"
68: height="100%"/>
69:
70: <!--Remove Button-->
71: <mx:Button label="Remove"
72: click="removeSnippet()"
73: enabled="{ isSelected }"
74: width="100%"/>
75: </mx:VBox>
Tags: advanced flex, as3, Snippets




Leave a Reply