I have gotten to work on a couple flex applications lately. Mostly just a pretty GUI for some data stuff. Normal CRUD operations. When you get right down to it is your average boring stuff.
So I decided that I would see what is required to get a couple of the popular CF ORM tools to work with Flash Remoting. I first implemented ARF! then Reactor. But not satisfied with the game I played I proceeded to set up ColdSpring with the 2 frameworks so I could use the Remoting Tools built into that framework.
Before I get into using the tools, I mentioned above, I think it would be a good idea to cover some basics on making remoting work.
Flex has made flash remoting a trivial chore. By using the mx:RemoteObject tag it is very simple to access your cfc and pull out your data. A simple remote Object looks like this:
source="/simpleRemoting/flashRemotingResponder"
result="resultHandler(event.result)"
fault="faultHandler(event.fault.faultstring)"
id="ro" />
The end point attribute is your domain plus the flash gateway url. The source is then the path to the cfc you are trying to call. The livedocs will explain the other attributes if you are trying to make this work
With this set up in your Flex file you could activate a remote method like :
Now lets just for a minute take a look at what we are actually calling on the other ColdFusion backend. We might have a cfc that has a function that looks something like this.
access="remote" returntype="query">
SELECT * FROM USERS;
That is all that is required in CF to return this data to flex. In our remote object above we reference a resultHandler() method that should deal with our results when they come back. An example of how our result handler could work might look like this:
public var users:Array;
public function resultHandler(results:Array):Void{
users = results._items;
}
The code above uses an array called users as the dataProvider for a data grid in flex. In the resultHandler method we populate the users array with the results of our remote call. In other words we set users equal to our members from our query.
Now if you are new to this you might wonder how we were able to set an array object with the results of a query. Part of the magic that happens in our remote gateway, is that when we return a query from a cfc, it actually turns that into an array of objects.
The trick then is getting the array of objects to be an array of a particular kind of object. And to do this justice I will just refer you to the article that explained it to me over on dirk’s site.
But to summarize what needs happen there are 3 steps. The first is that your object that gets returned from CF needs to have a public property called “_REMOTECLASS”. The value of that is the full path of the cfc. The second and third pieces has to do with configuring Flex. I use a function that gets called on initialze of my application. In this you set the static property that the Flex remoting classes use to identify what type of object is coming from cf. I use something like this.
var dep1:path.to.vo.Member;
public function initApp():Void {
mx.utils.ClassUtil.FLEX_CLASS_FIELD = "_REMOTECLASS";
}
The last peice of the magic that happens in the flex remoting gateway, is to have a dummy var that is the AS type that your cf object should be mapped to. I have included this in the code above. Then the third peice has to do with how your AS object gets mappend when you send data back to the CF gateway. In your AS class you need to use the registerObject line and map the cf type to the as object.
static var registered=Object.registerClass("path.to.MemberTo",path.to.vo.Member);
And with those pieces in place you should get your objects returned as the correct types. I will be showing examples of this as I publish the code for the various versions I have created. So stay tuned.