FlexFusion

April 14, 2008

Returning typed objects to Flex from ColdFusion

Filed under: ColdFusion, Flex, code — Tags: , , , , — Gareth @ 12:22 pm

Back in January I saw Brian Kotek’s post about returning typed structs to Flex. When I first read this article, I wasn’t sure how useful it was (I guess I was getting a little confused with the addition of Transfer and AOP in the article, and figured it was just something for that framework). A few weeks later I came across another article about the same idea, and something clicked and I realized just how brilliant this was.

Previously in order to return a typed object to Flex from Coldfusion, you would usually set up aliasing within both your ActionScript object and your CFC

AS:

[RemoteClass(alias="com.mySite.UserVO")]
public class UserVO {

CFC:

<cfcomponent alias="com.mySite.UserVO">

Then, when you transfer objects back and forth between ActionScript and ColdFusion, they are automagically converted to that specific type (along with all of the methods that go along with them). The only bad thing with this method, is that in order to transfer the CFC VO back from ColdFusion to ActionScript, you have to create that object in ColdFusion. Now this doesn’t create much overhead for one object, but if you query the database and return those query rows as separate objects, you have to createObject on each query row, which is an insane hog of memory and server resources. Plus, ActionScript cannot use any of the extra methods that CF is returning, so really anything other than the CFC object’s properties are just extraneous information.

Using the newly discovered technique, you can return the CFC object’s properties as structs rather than having to use createobject each time. You create the properties of the CFC as keys in the struct, such as

myStruct['firstName'] = "John";
mystruct['lastName'] = "Doe";

All that’s needed is to set one extra key in the struct e.g. myStruct['__type__'] = “com.mySite.UserVO” When the items are returned to ActionScript any struct that has __type__ as a key is automagically converted to an object of that type. What’s even more brilliant is if any items within the struct also have an __type__ (e.g. if the user has a company, and that company is returned as a company struct using the same __type__ methodology) then they also get converted to that typed object, so some sort of recursion is happening also.

Now for the sad news (on my part at least). After spending 3 days trying to get this to work on CFMX7 (I had read in a couple of different places that this supposedly would work in CFMX7), I finally found that it apparently only works via LiveCycle or CF8 (which has Flex Data Services Express, I think). After trying everything imaginable, I ended up just having to write a function to convert the returned data to objects. Now I’m hoping that my company will upgrade to CF8, but I don’t think that will be happening. It looks more likely to be Java + WebORB. I’m not sure if this same capability is possible with WebORB, but I’m hoping it does. Certainly is a nice feature to have. Anyway, hopefully this post will be informative for those with CF8 (or LiveCycle) and save those others with CFMX7 from going down the same path as me for 3 days :)

The original post with a little more details is on the CFTalk Mailing List

April 10, 2008

Weirdness with livedocs

Filed under: Flex, code — Tags: , — Gareth @ 3:12 pm

Lately there has been some strangeness with the Adobe livedocs for Flex. Every now and then the page will just constantly reload in a never ending cycle. This seems to only be happening on on the Flex 3 livedocs.

Update:
Looks like the Flex Doc Team will be fixing the problem soon and will get it working as it once was. They even posted a link to a downloadable version of them (It’s 67MB so be prepared for a small wait). I prefer to use this method for access any documentation rather than going online to access them mainly due to the fact if I’m out and about and don’t have an internet connection (I know, not very common any longer, but still…), then I still have access to the documentation. Plus, the livedocs weren’t exactly speedy to begin with, as they had to load the javascript and frames and all of the other fun they’ve put in there. The local version is nice and quick to open. Hopefully I’ll get a chance soon to write an AIR version using the Flex documentation (something like CFDocs which is very quick and has a nice interface)

April 9, 2008

isDate function for Flex…found!

Filed under: Flex, code — Tags: , , — Gareth @ 9:12 am

Well, sort of…

ColdFusion has a very nice “isDate()” function that allows you to pass in a variety of formats, and check whether the passed in value is a date. I had been searching for something similar in Flex, but had very little success until I started playing around with different functions myself.

I had been writing a custom controller for a datagrid which allowed passing in data from an object or table and displaying it in an editable datagrid. The problem I ran into was that depending on the data returned, trying to set those values back to the object that I was using, caused me some issues (such as trying to set a date stored in a text field, back to the object as a “new Date” value). After trying many different kinds of things, I found that the parse function of date works very nicely.

You can do something like

    if ( Date.parse( myDateField.text ) ) {
        myObj.recordingDate = Date.parse( myDateField.text );
    }

If you pass it a value that is not a valid date, the statement returns false.

Anyway, I had been searching around for a while trying to find something that would allow me to check for isDate and this fit the bill nicely. It also allows you to pass in quite a varied type of date formatting and it still recognizes it. Very powerful feature, it seems.

Déjà vu?

Filed under: Site Updates — Tags: — Gareth @ 9:05 am

I have started to copy over some of my blog posts from ColdFusion Community in order to keep everything that I’m typing localized in one spot.  Hopefully this will save me from having to search 2 places when I’m trying to figure out an answer to something I’ve already solved previously.  So, if you have a feeling of Déjà vu, it may be you were enjoying my nonsense before.

Powered by WordPress