explorers’ club


Tutorial: AppCoreLib’s XMLDataBroker, mock XML data & Cairngorm
Monday, May 12, 2008, 6:46 pm
Filed under: actionScript, appCoreLib, cairngorm, client side, development, flex, projects, server side, tutorial, xml

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.



AppCoreLib: Finally got the ASDocs uploaded
Wednesday, March 26, 2008, 6:21 pm
Filed under: actionScript, appCoreLib, development, flash, flex

Nothing more that that. Check em out here:

AppCoreLib project home – link
AppCoreLib docs – link



Better documentation for AS3’s XML class (e4x)
Wednesday, February 27, 2008, 4:25 pm
Filed under: actionScript, appCoreLib, development, e4x, flash, flex, regexp, xml

A powerful yet elegantly simple construct

As a flex developer, I consider XML as a necessary and highly useful language for various aspects of RIA development. There is a myriad of uses including but not limited to:

  • external URL storage for links
  • non-embedded content loading of jpgs, gifs, pngs and other swfs
  • internal URL pointers for RESTful type services
  • incoming service response objects
  • application initialization data (e.g. dataProviders, default settings)
  • and the list goes on

That is all well and good. But the problem is that if you don’t fully understand how to traverse an XML object and understand what it does when using e4x to access, then you are not making the most of this simple yet powerful development structure. The next problem lies in that there is very poor documentation on e4x in respect with the ActionScript XML class.

Truly understanding what the XML Object & e4x does

Adobe did a decent job of showing you how to traverse an XML object via e4x and then seeing their simple little trace statements. But what they failed to do was to give you useful examples of how to use that output and turn it into ActionScript-friendly objects.

Grabbing an XML node’s attribute

We have all probably used the following to get a value from an attribute from an XML node:

var o:Object = someXMLObject.@someAttribute; //o is now an XMLList

The issue with this example is that o is an instance of an XMLList. In order to fully make use of the value inside that singular attribute (why it turns it to an XMLList when specifying that node’s attribute expecting a singular value, I have no idea) we have to cast that value to what we want. Here are a few examples:

var s:String = String(someXMLObject.@someAttribute);

var n:Number = Number(someXMLObject.@someAttribute);

Easy!

Constructing an XML object from within ActionScript

Most everyone has probably used the following to construct an XML variable in AS3:

var x:XML =

<node someAttribute="value">

   <subNode anotherAttribute="value"/>

   <subNode>value</subNode>

</node>

Doing a trace on that would yield the expected XML to output to an XML string, retaining the nested structure and brackets. But using the new XML() constructor can yield some different results, some unexpected and not entirely useful.

var x:XML = new XML("node"); //traces "node" and not <node/>

So how would one dynamically construct a new XML object when not knowing the node name?

var x:XML = new XML("<" + dynamicValueForNodeName + "/>");

I don’t like the above setup. Its a messy read. Certainly there is another option?

var x:XML = <genereicNodeNameToBeChanged/>;
x.setName("newNodeName"); //traces <newNodeName/>

While this is documented, they really don’t touch on it at all in their given examples.

Setting values to nodes

If you wanted to put a subNode inside a parentNode you’d probably use syntax such as:

var x:XML = <node/>;
var x2:XML = <subNode/>;
x.appendChild(x2); //which yields <node><subNode/></node>

That is probably the most common usage of appendChild(). But did you know you can append values as well? Not just subNodes but also non-nodal values:

var x:XML = <node/>;
x.appendChild("someValue"); //traces <node>someValue</node>

Again, this is very useful but not really touched upon by the documentation.

Finding values using the . syntax

One of the nicest features about e4x is that you can rapidly find value(s) nested deep within the XML by using the “dot” syntax. I will abstain from listing an example here as there are numerous examples on the documentation site. But did you know that you can make multiple inquiries on an XML structure within the same line of code?

arrayElementType = String(item.metadata.(@name == "ArrayElementType").arg.(@key == "" || @key == "type").@value);

This eliminates the need to make multiline inquiries by getting an XMLList from one e4x boolean operation, then repeating down the line until you have driven down into the specific node you are looking for.

Using this new-found knowledge

Though you may or may not have known about some of these little tricks, you might be wondering, “so where do I use this”. A few posts back I eluded to the fact that I was developing an XML-to-ActionScript translator for AppCoreLib. The translator would then be able to quickly take in an XML response from a web service and spit back a useful ActionScript object of a specified class with little work involved by the responder. In order to do this, I have to make major use of XML and specifically the XML spit out by the describeType method. It means that I not only have to traverse the XML object, I also have to dynamically construct XML objects to be sent back. After the first build, I was a little miffed that I had some hard to read, and plain ol’ ugly ass code. I wanted to fully immerse myself in what the XML object and e4x had to offer.

If you have some not-so-well-documented tips and tricks for XML and e4x, send them along and I will add it to the post.



AppCoreLib
Friday, December 28, 2007, 9:44 pm
Filed under: actionScript, appCoreLib, contracting, development, flex

Preface

For the longest time I have been using a little set of helper classes that I have just recently named AppCoreLib.  I have used this on various projects of varying sizes.  Everything from Enterprise level apps to my pet projects.

Though not entirely original, the idea hasn’t really been put into an open-source swc that I know of.  So I decided to do so.  Thanks and credit goes out to my bud EKZ who spawned the idea in this particular flex-flavor.

Application Core Library

AppCoreLib consists of a few lite-weight classes.

  • broker based classes
  • a special event class
  • an  application interface

The idea is this: Ofttimes an application needs to load various content and settings files prior to the application’s view being shown.  The reason is that if you are using an MVC framework with {bindings} and/or other mechanisms on your model, you may encounter null pointer exceptions if a view tries to load prior to content dependencies being loaded.  Enter AppCoreLib.  It provides a very lite-weight framework added at the <application/> level of your RIA.

How it Works

  • default flex loading mechanism loads swf
  • IApplicationShell implementor’s creationComplete handler instructs instances of the broker classes to retrieve data dependencies
  • broker classes notify handlers that they have indeed loaded their data
  • upon all dependencies being loaded, IApplicationShell.shellInitiliazed() is triggered where normally you would put your RIA’s UI classes to be loaded.
  • Since certain broker classes are Singleton implementors, you can easily access their data from anywhere in your application.

Check It Out

I have set up the project home at google code.  Here are some various links: