This page is an archive of my old blog. Please visit for my current blog.
This site is no longer being maintained and commenting is disabled.

Getting Started with Cairngorm – Part 1

I spoke about Cairngorm 2.2 in the Flex Bootcamp at Max this week. Many people were interested in Cairngorm, but I only had about 10 minutes to explain the basics of Cairngorm. I guess the easiest way to assist these people is to do a quick blog series on the benefits of Cairngorm. This series will combine articles with "code-along" videos.

Disclaimer: I do not claim the be "the expert" on Cairngorm - I am far from it. However, I have used Cairngorm on several large projects (both at Georgia Tech and in my own business). I am certainly open to corrections if you see that I have made an error on this project. If you want "the experts" check out: Steven Webster, Alistair McLeod, Alex Uhlmann, and Peter Martin.

Note: The guys at Adobe Consulting (that developed Cairngorm) are currently investigating some new things with the framework as a whole. It is possible (actually probable) that some of these things will change in the future. One of the very specific areas of change is the Model Locator.

Part 1 – Getting Started By Using a Model Locator

The Model Locator pattern is used in Cairngorm, but you don't have to have a full Cairngorm Implementation to use the pattern. First, let's cover what benefits that you get from using a Model Locator.

A Model Locator is a centralized repository for all of the data that is needed across your application. Your data will exist inside of a "singleton class". This "class" can only have one instance of itself. Why is this important? Let me give you an example.

We have great mini-notepads at work that I use to write down data while I work. However, sometimes, I lose a notepad – get a new one, and then find the old one. After both have been heavily used – it is really hard to find out which notepad I used to write down a piece of information from one week ago. That is a simple example – but imagine if I had 20 notepads? This could get crazy.

In the same way you could have a "class" that gets "instantiated" 20 times within your application (even if you don't mean for it to). The easiest way to eliminate this problem is to use a "singleton". A singleton is a class that is never "created" in the traditional way. It has one main rule – no more than one of itself can exist at any point in time. How does it do this? I will show you in the Model Locator example.

  1. package net.davidtucker.CairngormSample.model {
  3.     import com.adobe.cairngorm.model.IModelLocator;
  5.     [Bindable]
  6.     public class ModelLocator implements IModelLocator {
  8.         // Single Instance of Our ModelLocator
  9.         private static var instance:ModelLocator;
  11.         public function ModelLocator(enforcer:SingletonEnforcer) {
  13.         if (enforcer == null) {
  14.                 throw new Error( "You Can Only Have One ModelLocator" );
  15.             }
  16.         }
  18.         // Returns the Single Instance
  19.         public static function getInstance() : ModelLocator {
  21.             if (instance == null) {
  22.                 instance = new ModelLocator( new SingletonEnforcer );
  23.             }
  24.             return instance;
  25.         }
  27.         //DEFINE YOUR VARIABLES HERE       
  29.     }
  30. }
  32. // Utility Class to Deny Access to Constructor
  33. class SingletonEnforcer {}

This code may look a bit daunting in the beginning, but trust me that it is not as difficult as it may appear. First, we have our package definition and we import some classes. Right now we know that we will need the IModelLocator interface. To use this you will need the Cairngorm SWC that can be found here: Cairngorm. Also note - you could build a model locator without Cairngorm, and I do this frequently on small projects (you just leave out the 'implements IModelLocator' and 'import com.adobe.cairngorm.model.IModelLocator' code from the Model Locator).

  1. [Bindable]
  2. public class ModelLocator implements IModelLocator {
  4.      // Single Instance of Our ModelLocator
  5.      private static var instance:ModelLocator;

Next we have our class definition. It is important that you use the Bindable metatag directly above your class definition. This will allow all of our variables that we define inside of the Model Locator to be used for binding. We also will go ahead and create one variable. It will be called "instance" and it will be of type ModelLocator. This will be the variable where we will store our one instance of our class. It will also be denoted as a "static" property. If you are not sure what a "static" property is, it's ok - we will discuss that in our next lesson.

  1. public function ModelLocator(enforcer:SingletonEnforcer) {
  2.      if (enforcer == null) {
  3.           throw new Error( "You Can Only Have One ModelLocator" );
  4.      }
  5. }

This is followed by the constructor. The constructor takes on argument - "enforcer". You will notice that this "enforcer" has a type of "SingletonEnforcer" which is defined directly after our class. Here is the logic behind that:

  • When you put a class in an Actionscript file below the main class, it is only available to that class. Many people refer to these as "utility classes" (although many people use that term in a much broader scope).
  • If the constructor requires this argument - then only our main class can create an instance of itself, because we do not have access to the "SingletonEnforcer" class - only the main class has this access.
  • We will not access our class in the normal way by using the "new" statement because we can't call the constructor (I will show you how we will do it in a bit).

Once we get inside of the constructor, we have a few lines that make sure things work as planned. The "if" statement ensures that we had a valid "enforcer" passed in. If there wasn't it throws an Error stating that "You Can Have Only One ModelLocator".

  1. // Returns the Single Instance
  2. public static function getInstance() : ModelLocator {
  3.      if (instance == null) {
  4.           instance = new ModelLocator( new SingletonEnforcer );
  5.      }
  6.      return instance;
  7. }

The "getInstance" function is how we will access our ModelLocator from our application. This function simply passes back the "instance" of the class. If it doesn't exist yet, it creates it. We can now get the ModelLocator by using the following code:

  1. var model:ModelLocator = ModelLocator.getInstance();

Video Example - Getting Started and Building a Contact List

Application Code
Download (418 kB)

125 Responses to “Getting Started with Cairngorm – Part 1”

  1. This blog is very informative too. I love blogging when it is for or about something I believe in. I also read news blogs often and find that it makes me feel more intelligent every time I read them. I will definitely put your site on my speed dial.

  2. Deba says:

    can anybody tell me how event is dispatched from command to view

  3. Harry says:

    Any plans to write a tutorial for cairngorm 3.
    A modular application.

  4. gesse says:

    thanks, I was looking it

    From Brasil

  5. Apurv Khadamkar says:

    good & informative Explaination of singleton class with real world example of the mini-notepads.

  6. RAVI says:

    Very nice tutorial…………….

    sir i have one problem…….
    instead of listing the people in .. i am using datagrid… to display firstname in FIRST column and secondname in SECOND column … but i getting error …………..
    con you suggest me the solution pleazzzzzzzzzzzzzzz.

  7. Pham Huy Anh says:

    Thank you! It helps. Just a question:

    Cairngorm is now at v.3, it’d be greate to have a sample for that

  8. I saw something about this topic on tv final night. Great article.

  9. Great write-up, I am a big believer in writing feedback on blogs to help the weblog writers know that they’ve additional something advantageous to the world wide internet!

  10. ragav says:


  11. Thiago says:

    muito bom o tutorial…parabéns

  12. I like your topic .it is interesting for provides nice infos so I’ bookmarked it.thanks for posting it

  13. I really like your topic and appreciate your is so interesting for me because it provides nice infos so I’ bookmarked it.thank you for sharing this topic

  14. I am cb receipt.I feel very lucky to discover your blog.I really appreciate your hard work.thank you very much for posting about this.

  15. Hi everyone I am a adwords voucher addicted.I really appreciate this blog.thanks for sharing those instructive posts

  16. What a great blog. I appreciate your work. If only you talk about how to refinance student loan it will be amazing.anyway thanks for sharing

  17. I don’t quite understand what you mean exactly by implementing RSS. Could you go into detail about what you’re looking for?

  18. Vishal says:

    Thank you. Simple and precise. Works for the Version 3 as well …..

  19. Hello David,
    Great work.This is really very simple and easy to understand.Thanks a waiting for your next tutorial for cairngorm 3.

    Manpreet patil

  20. thanks admin
    nice blog great document!!!
    great effort thanks for sharing
    Medyum Niyazi

  21. getting started says:

    Hi, a question on the structure of projects.
    I noticed the cairngorm 2.2 swc is in a bin folder (inside the zip)
    Whay is this? Should I put the swc in the bin folder of the project.
    If so what is the idea of this. I already know how to link to the swf, just wondering about best practise and why

  22. Komo says:

    In Flex/Flash Builder, right-click your project, select properties, Flex Build Path and in the Library path tab, click Add SWC and select the Cairngorm.swc which is in the bin folder.

  23. Janet Nolt says:

    I simply adore your site WOW! I had a great idea for your interesting blog my friend :) . I’m a blogger too and I spend a lot of time making and reading sites like » Blog Archive » Getting Started with Cairngorm – Part 1 all day. I used to just waste my energy making a few bucks here and there from google adsense but i found something SO much more powerful and better. After learning the proper way to rank and get visitors (so easy it’s hard) $348.15 $$CASH$$ in the last 3 days just days after modifying my blogs. Can you imagine how floored I was? Here is where I learned all my tricks—> <—Just giving your site a look I can tell you'll probably pull in a few hundred bucks a month if you modify just a little code. I hope that it serves you well and you have all my blessings. Cheers! Janet Nolt

  24. David Cohen says:

    Jesus, somebody call ma, really love this blog :-) ! But hey bud, I got some bad news, you’re really missing out of a lot of visitors. I’m a blogger myself and I spend a lot of time making and reading them, ones like » Blog Archive » Getting Started with Cairngorm – Part 1. I recently got a new tool and it’s done *wonders* to my entire business, this crazy thing is SO much more powerful and better. It’s only been a week but my revenue has gone up to $258.11 $$CASH$$, all in one day! So want me to do you a favor? Ok, here’s my big secret, where I learned all my tricks: –>> <– Just giving your site a look I can tell you'll probably pull in a couple hundred bucks a month if you just spend a few minutes tweaking some stuff. Damn, I feel like I just made someone's day today. But keep it a secret! I don't want too many people using it. David Cohen

  25. [...] Artigo sobre Cairngorm parte 1 e o vídeo Cairngorm Part 1 [...]