Monday, July 13th, 2009
So I’ve been publishing more and more of my code up on Github, yes I will SVN it up to Google Code too! There are a few classes that people may not know how to use or why to use them, so here’s a quick run through of the ones that reside in “com.firestartermedia.lib.as3.data“:
DataService.as
This is a parent class, one that you’ll never need to instantiate, rather you would use on of the children class. Nevertheless, it simply acts as a quick and simple text-based data service-wise, if you see what I mean. It handles the creation of a loader, “URLLoader()” to be exact, and also handles the events that come with it. Once nice thing about this parent class is that it asks the children to specify two types of events once the loader has loaded the data:
- loadedEvent: this is simply an event to tell you that the data has been loaded, but not yet parsed.
- readyEvent: and this event tells you that the data has been loaded and parsed.
This simple set up allows you to declare the variable “dataType” to either “TYPE_XML” or “TYPE_JSON” and the data service will do the hard work for you.
Now you may be thinking to yourself why the “loadedEvent” is of any use, well, imagine if you wanted to do something to some XML or JSON before you passed it back to your proxy, and this business logic would come in handy with other projects, this is where you would hook into the data service before it sends out the “readyEvent”.
XMLDataService.as
This class is a child of the data service class and simply acts as a nice interface to load some XML, for example, this is all you’d need to do:
{
var service:XMLDataService = new XMLDataService();
service.addEventListener( DataServiceEvent.READY, handleVideoDataGetReady );
service.send( 'http://localhost/xmlTest/get' );
}
private function handleVideoDataGetReady(e:DataServiceEvent):void
{
var data:XML = e.data as XML;
...
}
So without having to faff about with URLLoaders and URLRequests, the class does it all for you. Further more, it allows you to send some variables, through either GET or POST, like so:
{
var service:XMLDataService = new XMLDataService();
var vars:URLVariables = new URLVariables();
vars.var1 = 'test';
vars.var2 = 'test';
vars.var3 = 'test';
service.addEventListener( DataServiceEvent.READY, handleVideoDataGetReady );
service.send( 'http://localhost/xmlTest/get', vars ); // GET
service.send( 'http://localhost/xmlTest/get', vars, 'POST' ); // POST, thus...
service.sendPost( 'http://localhost/xmlTest/get', vars ); // POST
}
private function handleVideoDataGetReady(e:DataServiceEvent):void
{
var data:XML = e.data as XML;
...
}
Cool eh? Well I find it handier! And it’s allowed me to write my own service libraries, for example, if you take a peek in the “com.firestartermedia.lib.as3.data.gdata” path, you’ll see the “YouTubeGDataService.as” class, here’s a run through:
YouTubeGDataService.as
Now the GData service returns a simple Atom feed, but I find it handy to have a nice class such as this to handle all the business logic for me, one of the most handiest functions is the “searchPlaylistData()”.
This function allows you to search through the whole of a YouTube playlist for a specific video, if you don’t enter a search term, it just returns every video from that playlist. For those of you who have already used the GData service with YouTube you’ll notice that you can only retrieve 50 items at a time, well this is where these functions do their magic. It simple cycles through all entries, making call after call until it’s reached the end, and then it searches through the playlist, so here’s an example of it in action.
In that Flex based example, you can search through a certain playlist, this will make using the GData API much easier, especially if you want to search through a set of “approved” videos for a brand and so on.
Have a play, or even better, give me ideas of services you want to use, I’ll happily write more!