Cairngorm Generators 18

Posted by Daniel Wanja Wed, 21 Feb 2007 19:40:00 GMT

I looked into Cairngorm a while ago (version 0.95 I believe, pre-ModelLocator area) and didn’t like the fact that you couldn’t use Flex bindings at that time. I recently gave it a second look I liked what I saw. Using Cairngorm is at first very verbose but it provides you with a clear way of organizing your code, which is very beneficial on larger projects. Cairngorm is well documented and there are several nice examples available. Also checkout http://www.cairngormdocs.org/. So yesterday introduced Cairgorm on two Flex projects I am working on.

The first project is a larger Flex 1.5 project (65 actionscript classes, 75 Data transfer object, >100 Mxml views) that I need to migrate to Flex 2.0. I though “great time to start with Cairngorm”. So the first step was to look at all the user gestures for all the screens and create a list of events, map these events to commands, create 3 delegates and services to handle all the remote calls. On paper that was pretty fast to do, but I didn’t want to create all the Cairngorm supporting class by hand. Handily I found the following generator that is a PHP application:

Cairngen


It targets currently only Cairngorm 2.0 and not the newer Cairngorm 2.1. So after generate the supporting classes I had to manually do some changes. But thanks for this tool.

The second project is a smaller Flex application backed by a Ruby on Rails server. And I found the following Ruby on Rails Cairngorm generators at http://code.google.com/p/cairngorm-rails-generator/

Simply copy the generators to your ”/vendor/generators” folder, the generators folder will then contain the following generators:

cairngorm
command
delegate
event
vo
worbservice

Note there are several places in Rails you can set generators, but that did the trick for me. The I created a “src” folder in my Rails root folder and issues the following commands:

Then you can use these different generator commands to build the structure you require, the events, commands, delegates and more.
   ./script/generate cairngorm org/onrails/myspyder   
   ./script/generate delegate  org/onrails/myspyder server
   ./script/generate command   org/onrails/myspyder show_page_watch server
   ./script/generate event     org/onrails/myspyder show_page_watch 

I issued the generate script for each of the commands that application needs to support. The main controller now looks as follows (still very early in the development phase)

package org.onrails.myspyder.control
{
    import com.adobe.cairngorm.control.FrontController;
    import com.adobe.cairngorm.control.CairngormEventDispatcher;
    import org.onrails.myspyder.control.*;
    import org.onrails.myspyder.command.*;

    public class MySpyderController extends FrontController
    {
        public function MySpyderController()
        {
            initialiseCommands();
        }

        public function initialiseCommands() : void
        {
            // Application Tabs
            addCommand( ShowAccountEvent.EVENT_SHOW_ACCOUNT, ShowAccountCommand );
            addCommand( ShowPagesEvent.EVENT_SHOW_PAGES, ShowPagesCommand );
            addCommand( ShowSettingsEvent.EVENT_SHOW_SETTINGS, ShowSettingsCommand );            

            // Page List
            addCommand( ReloadPagesEvent.EVENT_RELOAD_PAGES, ReloadPagesCommand );
            addCommand( AddPageEvent.EVENT_ADD_PAGE, AddPageCommand );
            addCommand( ReloadPageEvent.EVENT_RELOAD_PAGE, ReloadPageCommand );
            addCommand( RemovePageEvent.EVENT_REMOVE_PAGE, RemovePageCommand );
            addCommand( SavePageEvent.EVENT_SAVE_PAGE, SavePageCommand );

            // Page Details
            addCommand( ShowPageWatchEvent.EVENT_SHOW_PAGE_WATCH, ShowPageWatchCommand );
            addCommand( ShowPageWatchResultEvent.EVENT_SHOW_PAGE_WATCH_RESULT, ShowPageWatchResultCommand );

            // Html Section
            addCommand( ShowOriginalPageEvent.EVENT_SHOW_ORIGINAL_PAGE, ShowOriginalPageCommand );
            addCommand( ShowPageSectionEvent.EVENT_SHOW_PAGE_SECTION, ShowPageSectionCommand );
            addCommand( SelectPageSectionEvent.EVENT_SELECT_PAGE_SECTION, SelectPageSectionCommand );

        }    

    }

}

Let me know your experience with Flex and Cairngorm.

Comments

Leave a response

  1. Alex MacCaw Thu, 22 Feb 2007 10:13:09 GMT

    Change all instances of ‘src’ to ’..’, then put the rails app in your Flex Builder (or whatever you use dir). This then generates all the code, without you having to copy anything over….

  2. Daniel Wanja Thu, 22 Feb 2007 13:46:20 GMT

    Thanks, that’s a good trick. Also the Cairngorm Rails generator does not overwrite files, so you won’t loose your changes when you add code to the generated files.

  3. ilya Thu, 01 Mar 2007 21:57:47 GMT

    Hi Daniel,

    i whipped up a rails generator for cairngorm during my last x-mass holliday in a few evenings.

    feel free to take a look and comment http://code.google.com/p/cairngorm-rails-generator/

    i still didn’t get around taking it a step further, but maybe you like the idea

  4. ilya Thu, 01 Mar 2007 21:59:15 GMT

    doh,

    i saw you actually write about it.

    how do you think we can improve it?

  5. forest Fri, 23 Mar 2007 15:43:10 GMT

    ilva,

    The generators are great. I have been very happy with them. They save lots of time writing boiler point code.

    daniel,

    Are you using cairngorm and weborb with your flex/rails application? It seems the rails controllers are not used at all, is this correct?

    Thanks, forest

Comments