wordpress flash API – sample code – Part 2

This post is part two of the AMF/PHP connector class that I started in a previous thread talking about the PHP part

now.. we go to flash. The first class is the DataConnector which establishes the connection to the AMF/PHP connector and calls the function
we declare a variable called callTitle which is assigned based on which function we’re calling. this variable is then used to decide what action to do in the event listener.

package data
{
	import events.Communicator;

	import flash.events.*;
	import flash.net.NetConnection;
	import flash.net.Responder;

	public class DataConnector extends EventDispatcher
	{

		private var gateway			:String ;
		private var callTitle		        :String = "";
		private var connection		:NetConnection;
		private var responder		:Responder;

		public function DataConnector()
		{

			gateway	= "./connectors/gateway.php";

			responder = new Responder(onResult, onFault);
			connection = new NetConnection;
			connection.connect(gateway);
		}

		public function getCategoryList(parentCatName:String = ""):void
		{
			callTitle = "getCategoryList";
			parentCategory = parentCatName;
			connection.call("functions.getCategoryList", responder , parentCatName);
		}
		public function getCategoryPosts(_catName:String = ""):void
		{
			callTitle = "getCategoryPosts";
			catName = _catName;
			connection.call("functions.getCategoryPosts", responder , catName);
		}

		public function getPostList():void
		{
			callTitle = "getPostList";
			connection.call("functions.getPostList", responder);
		}

		public function getPost(id:String):void
		{
			callTitle = "getPost";
			postID = id;
			connection.call("functions.getPost", responder , postID);
		}

		private function onResult(result:Object):void
		{
			switch (callTitle)
			{
			 	case "getCategoryList":
			 		result = result as Array;
			 		var _cat:Array = new Array();
			 		for (var i:int=0; i <result.length; i++)
					{
						var _obj:Object = new Object();
						_obj.id 	= result[i].id ;
						_obj.title 	= result[i].title ;
						_obj.desc	= result[i].description;
						cat.push(obj)
					}
					// now do something with that array
			 	break;

			 	case "getPostList":
			 	       result = result as Array;
			 		var _cat:Array = new Array();
			 		for (var i:int=0; i<result.length; i++)
					{
						var _obj:Object = new Object();
						_obj.id 	= result[i].id ;
						_obj.title 	= result[i].title ;
						cat.push(obj)
					}
					// now do something with that array
			 	break;

			 	case "getCategoryPosts":
			 	       result = result as Array;
			 		var _cat:Array = new Array();
			 		for (var i:int=0; i<result.length; i++)
					{
						var _obj:Object = new Object();
						_obj.index= result[i].index ;
						_obj.id 	= result[i].id ;
						_obj.title 	= result[i].title ;
						cat.push(obj)
					}
					// now do something with that array
			 	break;

			 	case "getPost":
			 		result = result as Object;
					// do something with the object
					// like trace(result.post.title) , trace(result.post.content) , trace(result.post.date)
					// and:
					// for (var i:int=0; i< result.meta.length; i++)
					// {
					//       trace(result.meta[i].index , " : " , result.meta[i].value);
					// }
			 	break;

			}
		}

		private function onFault(fault:Object):void
		{
			trace(String(fault.description));
		}
	}
}

This class can now be called from the parent class (Main for example) in the following fashion:

package
{
	import data.DataConnector;
	import flash.display.Sprite;
	import flash.events.*;

	public class Main extends Sprite
	{
		private var _dataConnector :D ataConnector;

               public function Main()
		{
			_dataConnector = new DataConnector();
			_dataConnector.getCategoryList("cat1");
		}
         }
}

the way I usually do its is follow this sequence:
Main -> DataConnector -> call Function
DataConnector ->receive Call result and handle Data
DataConnector -> Dispatch Event with the handled Data
Main -> receive the event in an event listener -> use the data.

but again, this sequence is very dependant on the structure of your classes and projects.. so now you’re on your own…

hope this was helpful enough

  1. daweed says:

    nice job… now with you’re API, we only have left the “template Tags” translation… ;)

  1. There are no trackbacks for this post yet.

Leave a Reply