nwebb

Flex, Flash, AIR

Cairngorm For Beginners (part 3)

In parts 1 and 2 of this article I explained how to get up and running with Cairngorm and I walked you through an example application which had been built using a prior release of the framework. In this part I want to go over some of the changes made in versions 2.1 and 2.2, and update the sample application to reflect those. Mercifully (for both you and my type-weary fingers) this should be a shorter post than the last two.

I have already made reference to some of the things that have changed - mainly deprecated interfaces. There is also a new helper class for events.

Okay where to start ...
You should be pretty familiar with those classes from the example by now, so let's get a feel for replacing some of the deprecated stuff. This is going to be pretty obvious to most of you, but I'll cover it for completeness. As an example we'll look at changes to LoginDelegate and LoginCommand:

Changing the LoginDelegate class
In part 2 I mentioned that Responder had been deprecated.
The Flex class mx.rpc.IResponder is now used in favour of the old Responder interface. This new interface specifies that the implementing class must have both a result and fault method. The changes are simple:

  • Change the import statement to: import mx.rpc.IResponder;
  • Change all references for Responder to IResponder. For example: private var responder:IResponder;
  • In the loginResult() method, change the methods called from onResult & onFault, to result and fault
  • Both result and fault expect a parameter, so for fault, send in null. For example: responder.fault(null);

This should leave you with one listed error still in the FlexBuilder 'Problems' panel, and it is an error in the LoginCommand class:

Changing the LoginCommand class
The error is a type coercion error, and the problem is with this line:

var delegate : LoginDelegate = new LoginDelegate( this );

The reason for the error is that we changed our Delegate class to expect a parameter of type IResponder to be sent in to the constructor, yet LoginCommand is still of type Responder. The changes required are as follows:

  • Change the import statement to: import mx.rpc.IResponder;
  • Change all references for Responder to IResponder
  • Change the method signatures for result and fault so that they are in line with the IResponder interface. For example: result( event : Object ) and fault( event : Object )

We can also substitute ICommand for the deprecated interface Command now. This is really easy and simply involves replacing Command with ICommand throughout the class - including in the import statement.

The other interface changes that I'm aware of are to ModelLocator (now IModelLocator) and ValueObject (now IValueObject), so feel free to go ahead and make those substitutions. However, note that in the example, Alex has specified his own class called ModelLocator, which resides in the package com.adobe.cairngorm.samples.login.model. This extends the ModelLocator which resides in the package com.adobe.cairngorm.model.ModelLocator . Take note of which one is being referred to before making any changes!

Event Dispatching & Using Clone()
In part 2 I mentioned that Cairngorm uses CairngormEvent and not flash.events.Event, and you may have noticed that Alex's example does not have a clone() method. Well CairngormEvent actually extends flash.events.Event so you may wonder why clone() wasn't used. After all, the Flash help files make it clear that you should override clone(). A good explanation of the need for overriding the method has been posted by Daniel Rinehart here. Here is a summarised quote (words in square brackets are my own):

...most of the time not implementing a clone() method won't hurt you but you really should create one ... it only becomes an issue if you relay [redispatch] an [custom] event

Also note that another small but nice addition to the CaringormEvent class is the data property, which can be used to hold information passed with the event in cases where the developer does not want to extend CairngormEvent.

Self-Dispatching Events
New to Cairngorm 2.2 is a helper class which makes event dispatching a little less verbose. Alistair McLeod posted about this here.

Actionscript:
  1. //Instead of:
  2. CairngormEventDispatcher.getInstance().dispatchEvent( loginEvent );
  3. //You can now do:
  4. loginEvent.dispatch();

I believe that's pretty much it with regards to updating the example file. I may edit this post in the near future if anything else is brought to my attention. Please feel free to leave feedback if you have any questions or simply found these posts helpful. Cheers.

19 Comments so far

  1. Jason June 21st, 2007 3:56 am

    These are some great posts. It would help us that are new to Cairngorm if you can extend the login in example by creating additional views, heck I would settle for one additional view.

  2. nwebb June 21st, 2007 9:16 pm

    Thanks Jason. Sure, I’m happy to extend the example if you think you’d find it useful, though adding a second view here would just be a repeat of the existing process (another button sending another event, which invokes another command…etc). It would probably be more useful to you to dissect one of the other examples provided by Alex (or to look at the code for the updated Cairngorm Store - which you can find via the Cairngorm wiki).

    Please note, I’m not going to have access to Flex until the start of July (the computer I’ll be using next week is fully locked down) so email me a reminder at the end of next week if you don’t see anything appear on the blog.

  3. pradeep July 18th, 2007 6:35 am

    aftr long google i found this article which make me clear cut understanding of cairngorm framework.

    please can you tellme how to make runnable (like exe file in c++)

    and install onthe desktop.

  4. nwebb July 18th, 2007 9:01 am

    Hi pradeep. The installation and setup instructions for Cairngorm are explained here: http://nwebb.co.uk/blog/?p=58

    Cheers,
    Neil

  5. [...] Webb has a few post labeled “Cairngorm For Beginners” that’s currently on its third part.  This part fixes some problems caused by updating to 2.1 and 2.2 of an application built through [...]

  6. Steven Rieger August 2nd, 2007 5:14 pm

    I”ve been through your articles and they really helped. Thank you.

    I have a project I’m working on that I am trying to get into Cairngorm. I slimmed it down to a simple view, model, command and delegate. However, I am using a webservice for my data.

    I am unable to get the webservice to fire. I am sure I’m doing something really stupid here. Anyway you can show a simple web service being consumed in a sample app? Better yet, can I send you an app and have you look at it?

    Thx

  7. gary August 3rd, 2007 2:22 am

    One thing that you missed is that in the “login” function you need to remove token.resultHandler = responder.onResult and token.faultHandler = responder.onFault and change it to: token.addResponder(this.responder)

  8. [...] So I decided to write it down on my blog. It based on Alex Uhlmann’s CairngormLogin Sample modified by Neil [...]

  9. nwebb August 28th, 2007 12:31 pm

    Hi Steven,

    the comments section doesn’t really facilitate code. Do you want to send me that app and I’ll take a look?

  10. nwebb August 28th, 2007 5:32 pm

    Thanks Gary, well spotted :]

  11. steven rieger August 28th, 2007 6:47 pm

    I have figured out how to get things working with webservices. It’s certainly not for the faint of heart LOL. I still have much to learn about Cairngorm but I am definitly making progress.

    Thanks for your offer.

    Cheers.

  12. nwebb August 31st, 2007 12:07 pm

    @Steven, check out my August 2007 posts which include more info on using WebServices and HttpServices with Cairngorm - hope they will be of some help.

    @Gary, my latest example now includes an updated version of the example with addResponder - thanks again for pointing that out. I hadn’t really looked at the commented-out code and that was a complete oversight on my part.

    Cheers guys.

  13. Matt April 9th, 2008 12:37 pm

    Great post, it’s helped me resolve some of the issues I’m having trying to implement in 3.0.
    One question, I am trying to implement a flash application using the Cairngorm architecture and the whole movie is wrapped up in c# wrapper application with custom functionality for serializing and deserializing objects between .net and flash using the external interface. My question is should I / can I create my on service which is called from the delegate or should I just carry on calling the external interface directly?
    Cheers.

    [Hi Matt, good question. From a Cairngorm developer perspective only, if I were coming to work on an existing project I would expect to find the business logic in the designated place (one of the advantages any architectural framework confers), and so if it were not too convoluted, I'd be tempted to create the service. However, the great thing is that you don't have to be constrained by the framework and obviously a single framework can't possibly suit all scenarios, so I'd say, if you feel the code is clean as-is, and the structure you have in place is working well, there may be no real reason to change it. Some people may disagree, but I think it's easy to become overly concerned with doing absolutely everything to the letter, and this often impacts on productivity. As long as you don't negate the benefits by deviating all over the place, I don't see it as a huge deal.]

  14. ashok April 24th, 2008 7:36 pm

    hi,
    this is a great post and I know Neil has taken a lot of efforts. wanted some help from anyone out here, who has tried to use BLAZEDS, with Tomcat and Cairngorm, as a UI layer. My specific questions are

    1. are there any configuration changes that we need to make to Tomcat when we use cairngorm. for the life of me, I do not know why the service that i exposed isn’t being called, when I call it from the delegator. I am using this architecture for a my masters project and wanted some help really bad for this issue.

    It used to work, before I moved to cairngorm.

  15. Adobe Flex - Cairngorm « Panduramesh’s Weblog September 29th, 2008 10:46 am

    [...] Neil Webb :: Cairngorm For Beginners :: Covers version 2.2 :: Part 1 :: Part 2 :: Part 3 [...]

  16. anil4it September 30th, 2008 1:15 pm

    [...] Neil Webb :: Cairngorm For Beginners :: Covers version 2.2 :: Part 1 :: Part 2 :: Part 3 [...]

  17. Anilkumar September 30th, 2008 1:42 pm

    [...] Neil Webb :: Cairngorm For Beginners :: Covers version 2.2 :: Part 1 :: Part 2 :: Part 3 [...]

  18. Flex with Cairngorm « It’s all about RIA October 14th, 2008 6:21 am

    [...] Neil Webb :: Cairngorm For Beginners :: Covers version 2.2 :: Part 1 :: Part 2 :: Part 3 [...]

  19. It’s all about RIA October 14th, 2008 6:26 am

    [...] Neil Webb :: Cairngorm For Beginners :: Part 1 :: Part 2 :: Part 3 [...]

Leave a reply

Bad Behavior has blocked 1077 access attempts in the last 7 days.