We've been working with several development teams over the past year on Flex applications using the Cairngorm framework from iteration::two. What might interest Flash folk is that we've also worked on multiple Flash applications using the same framework with a few minor modifications - and achieved great results. Now Flex is excellent and there are some jobs that I wouldn't contemplate doing in Flash, but a lot of projects are highly do-able in Flash if you take a leaf out of the Flex programmer's book and use the frameworks and patterns they use.
Some of you may remember my previous blog post containing a sample Flex/Cairngorm/ColdFusion contact application. I was asked to present at the inaugural Sydney Flash Users Group a month or so back, so for something to do I decided to port this sample application to Flash/Cairngorm and discuss the differences. Here's the application:
As you can see it looks like a Flex application, thanks to a nice little re-skinning of the UICS2 components I saw mentioned on Brajeshwar's blog. Starting with the original Flex application here's what I did to get the Flash version:
Create com.rocketboots.contact.views.Application.fla (I count the fla as a special entry-point "view").
Place an instance of the Flash Remoting classes on the stage
For each mxml tag, create a symbol with the appropriate controls laid out on it and a corresponding view class.
Create our own version of the Cairngorm ServiceLocator class that doesn't extend Flex's UIObject class.
Create a FlashServices actionscript equivalent of Services.mxml.
Modify the delegates to use FlashServices and mx.rpc.RelayResponder instead of mx.utils.Delegate.
Remove a reference to the alert class in the view helper
About 70% of the code remains unchanged between the two versions. You can download a patch for the original source code and try it yourself - this replaces the com/rocketboots/contact package hierarchy with a version incorporating the above changes. Make sure to change the class path in the fla's publish settings to the directory containing the com package, and that the service url is correct in FlashServices.as - feel free to use "http://www.rocketboots.com.au/flashservices/gateway" if you don't have a CF installation to try it out on.
The next project is to create ARP versions of the Flex and Flash sample applications...
Comment made by martin / Posted at Thursday 16 June, 2005 03:06
I think that the filter should be case insensitive :)
Comment made by Rostislav / Posted at Thursday 16 June, 2005 05:06
Done :-)
Change com.rocketboots.contact.business.ContactFilter lines 41-42 to:
if ((contacts[i].FIRSTNAME.toUpperCase()).indexOf(criteria.toUpperCase()) != -1 || (contacts[i].LASTNAME.toUpperCase()).indexOf(criteria.toUpperCase()) != -1)
And that should do the trick.
Comment made by Robin Hilliard / Posted at Thursday 16 June, 2005 06:06
Hi
Thank you very much for that interesting piece of information.
One question: Is it possible to build your sample with mtasc. Do I have to modify the .fla file?
Regards
Comment made by picnic / Posted at Thursday 16 June, 2005 11:06
You don't need to modify the fla - all the code is in external classes except for a single call to the ApplicationView.init() method in one of the frames.
I'm very interested to hear if this works - I've had mtasc installed for a while and use aseditor in eclipse, just haven't got around to trying it out.
Cheers.
Comment made by Robin Hilliard / Posted at Friday 17 June, 2005 12:06
Hi robin
I'd like to report my experiences regarding mtasc/cairngorm. First I have to say that I'm more of a beginner with AS2 and Mtasc.
1. I had troubles to "merge" your patch with the cairngorm framework. I had to get the missing files (eg. the control.ControllerFront.as) manually and redefine classpathes and import pathes. I think this was the opposite of "best practice", but it worked and mtasc was satisfied.
2. Then I got into real problems. Code "/*
Listen for changes to criteria text
*/
var criteriaListener = new Object();
criteriaListener.owner = this;"
MTASC Error Message: "type Error unknown variable owner".
Now this is where i stucked... Maybe you have some ideas?
Regards Ben
Comment made by picnic / Posted at Friday 17 June, 2005 02:06
Hi Ben,
I probably should have mentioned that this was built with Cairngorm 0.9, not 0.99. The steps are (1) download the original source from my previous post (2) Download Cairngorm 0.9 and install it in the com directory next to rocketboots (com.iterationtwo.cairngorm..) (3) Download the patch and install in com.rocketboots.contact... Sounds like you may have ported it to 0.99 for me - thanks :-)
Re the error, the Macromedia Flash compiler also has this annoying feature, where it won't allow you to add a property dynamically to a "vanilla" object unless you trick it, as I've done on line 14 of ContactListFlashView.as. This trick isn't working in mtasc, perhaps add "var owner : Object" as a local variable after line 21 and see if that fixes it.
Robin
Comment made by Robin Hilliard / Posted at Friday 17 June, 2005 08:06
BTW, a much better way to do this would have been to have changeCriteria() and changeGrid() methods in ContactListFlashView.as, then replace lines 26-48 with
criteriatxt.addEventListener("change", mx.utils.Delegate.create(this, "changeCriteria"));
grid.addEventListener("change", mx.utils.Delegate.create(this, "changeGrid"));
Comment made by Robin Hilliard / Posted at Friday 17 June, 2005 09:06
The response from mtasc to your last solution was "last type error String should be Function", so I tried it with the local variable and it seems to work so far. Now, the next error I get is "ContactListFlashView.as:49: characters 3-31 : type error Unknown variable EventBroadcaster". Because I think the line nr didn't match with yours anymore, I give here the context:"var gridListener = new Object();
gridListener.owner = this;
gridListener.change = function(event) {
EventBroadcaster.getInstance().broadcastEvent("selectContact", event.target.selectedItem);
}
grid.addEventListener("change", gridListener);"
Comment made by picnic / Posted at Friday 17 June, 2005 05:06
Apologies for the delay. EventBroadcaster is a Cairngorm class - make sure that the import statement has been updated to reflect the 0.99 package naming.
Comment made by Robin Hilliard / Posted at Monday 20 June, 2005 08:06
Hi,
I tried to connect to your flash service gateway as instructed in your blog:
http://www.rocketboots.com.au/blog/index.cfm?mode=entry&entry=83219052-E081-51EF-A73B5F4EF642C3F5
and I'm having difficulty to understand how it all works as Flash actually spit out error:
Error opening URL "http://www.rocketboots.com.au/flashservices/gateway"
from the FlashServices.as
Is there something wrong with the flash service or actually it's something wrong with my configuration.
Thanks for the help
Comment made by Johanes / Posted at Thursday 29 September, 2005 02:09