explorers' club

explorations in dev, science, sci-fi, games, and other fun stuff!

Tutorial: AppCoreLib’s XMLDataBroker, mock XML data & Cairngorm

6 Comments

The potential of Flex for rapid prototyping and general application development is amazingly fast. With some mock data and a basic user-experience (UX) scenario, a developer can create a functioning sample application in a matter of minutes.

If you are developing either a full blown RIA or a prototype application then you have probably at least heard of Cairngorm. If not, familiarize yourself with the basics here – link. I use a stripped down version of it for almost every project I have worked on. It works beautifully

I plan to show you an example of using AppCoreLib‘s XMLDataBroker to fill in some mock XML data for your commands. This is helpful when you have a basic idea of an API/services but they are not returning anything or not ready to be tapped or you wanna test out a service’s result format before the server developers implement it.

Before I continue, let’s set one or two rules about this tutorial.

  • the expected format for the returned data is XML
  • the mock XML data format will reflect the same format of the actual service’s result

Firstly let’s take a look at a basic Cairngorm command. This is a stripped down version so you won’t see any delegate-type references in here.

package com.foo.controller.commands
{
    import com.adobe.cairngorm.commands.ICommand;
    import com.adobe.cairngorm.control.CairngormEvent;
    import com.foo.business.Services;

    import mx.rpc.AsyncToken;
    import mx.rpc.IResponder;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.http.HTTPService;

    public class SampleCairngormCommand implements ICommand, IResponder
    {
        private var _vo:Object = {};

        public function execute (event:CairngormEvent):void
        {
            var call:HTTPService = Services.getInstance().httpService;
            call.url = "http://www.foo.com/someService";

            var param:Object = {};

            var token:AsyncToken = call.send(param);
            token.addResponder(this);
        }

        public function result (data:Object):void
        {
            var xmlResult:XML = ResultEvent(data).result as XML;
        }

        public function fault (info:Object):void
        {
        }

    }
}

So now with about 5 lines of code, I will show you how to inject mock XML data for creating a response that fits within the Cairngorm command structure:

package com.foo.controller.commands
{
    import appCoreLib.business.XMLDataBroker;
    import appCoreLib.events.XMLLoadEvent;

    import com.adobe.cairngorm.commands.ICommand;
    import com.adobe.cairngorm.control.CairngormEvent;

    import mx.rpc.IResponder;

    public class SampleCairngormCommand implements ICommand, IResponder
    {
        public function execute (event:CairngormEvent):void
        {
            var x:XMLDataBroker = new XMLDataBroker();
            x.addEventListener(XMLLoadEvent.XML_LOAD_SUCCESS, result);
            x.addEventListener(XMLLoadEvent.XML_LOAD_FAILURE, fault);
            x.loadXML("assets/xml/someMockXMLData.xml");

            /* var call:HTTPService = Services.getInstance().httpService;
            call.url = "http://www.foo.com/someService";

            var param:Object = {};

            var token:AsyncToken = call.send(param);
            token.addResponder(this); */
        }

        public function result (data:Object):void
        {
            var xmlResult:XML = XMLLoadEvent(data).xml;
            //var xmlResult:XML = ResultEvent(data).result as XML;
        }

        public function fault (info:Object):void
        {
        }
    }
}

That’s it! Simple, effective and most importantly, easy to remove once an actual service response is available.

About these ads

6 thoughts on “Tutorial: AppCoreLib’s XMLDataBroker, mock XML data & Cairngorm

  1. what’s up JWO!

    I’d take it a step further and incorporate a little more cairngorm, specifically the service locator, which I’d extend and then stuff the logic in there. And since you can serve up an xml file as an HTTP Service, it could be summarized by one line;

    httpServiceURL = isLive? “path/to/live_service” : “path/to/mockData.xml” ;

    The HTTPService returned to the command will have loaded that httpServiceURL and your result/fault handlers will get called since you’re adding the command as a token to the HTTPService (no need to manually add the listeners). Also, by centralizing the logic in the service locator, when the time comes to flip the switch for all commands to use live data, you make changes in one class, as opposed to several. In general, I’ve found that I avoid complications by keeping commands “in the dark” about such decisions : )

    -ekz
    ps: I have a DynamicServiceLocator that does something like the above for different service types and endpoints, maybe one day I’ll get to actually do some blogging and share it : )

  2. I hadn’t ever thought about using the HTTPService as the mechanism for loading the XML. Yeah that would simplify things considerably. Thanks for the suggestion. When you say you avoid putting that kind of logic in the commands, does this mean you have a one-to-one correspondence with each command, so for every command you have an HTTPService in your ServiceLocator?

  3. No, not always the case.

    Just that if , for example i have 2 different commands sharing an HTTPService that they get from the service locator. If I had conditional service logic (eg: Live Service vs Mock Data) at the command level, i’d have to change it for both those commands. Placing the logic in the service locator , at the time when the service is created (or retrieved, depending on what logic I’m implementing), allows me to do it centrally.

    -ekz

  4. well I’m not kidding when I say I learn something new everyday. That is why I love what I do. Thanks for the input EKZ. HOW COULD YOU TURN YOUR BACK ON OUR BELOVED APPCORELIB?!?!?!?!? J/K of course.

    Get that damn blog up and running so you can share your wealth of knowledge!!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.