Getting Started with Cairngorm – Part 2

October 18th, 2007

Recap: In Part 1, I discussed the basic implementation and use of the ModelLocator pattern. This pattern is one of many design patterns contained within the Cairngorm micro-architechture. This design pattern will be used in Part 2 as well, so it is assumed that you are familiar with the concepts in Part 1 of the tutorial. At this point, we still are not working with a "complete" Cairngorm application (that will come in Part 3).

Part 2 - Using a ModelLocator to Manage the View

Note: As with all of the tutorials that will come in this series, this lesson has two parts. First, in the article you will learn the theory behind the topic, and then in the video you will do an actual "code-along". The article will give some instructions in how to set up your project for the "code-along".

In the previous tutorial you learned the advantages of using a ModelLocator to manage data within an application, however, the advantage of the ModelLocator pattern extends beyond the management of data. It can manage the "view" of an application as well. To see how view management works in Cairngorm, you will first need to create a new project named "ViewManager" and name the main application file "Main.mxml". For this project, your will also need to add the Cairngorm.swc to your project build path (as described in Part 1). You will also need to create two new folders inside of the "src" folder: view and model. When you are completed, your project should look similar to Figure 1 below.

Cairngorm Project Window
Figure 1 - ViewManager Project

Next, you will need to take the ModelLocator code from the previous tutorial and place it inside your application. I have posted the code below for your convenience.

Actionscript:
  1. package model {
  2.     import com.adobe.cairngorm.model.IModelLocator;
  3.     [Bindable]
  4.     public class ViewModelLocator implements IModelLocator {
  5.         // Single Instance of Our ModelLocator
  6.         private static var instance:ViewModelLocator;
  7.         public function ViewModelLocator(enforcer:SingletonEnforcer) {
  8.         if (enforcer == null) {
  9.                 throw new Error( "You Can Only Have One ViewModelLocator" );
  10.             }
  11.         }
  12.         // Returns the Single Instance
  13.         public static function getInstance() : ViewModelLocator {
  14.             if (instance == null) {
  15.                 instance = new ViewModelLocator( new SingletonEnforcer );
  16.             }
  17.             return instance;
  18.         }
  19.         //DEFINE YOUR VARIABLES HERE
  20.     }
  21. }
  22. // Utility Class to Deny Access to Constructor
  23. class SingletonEnforcer {}

Example 1 - The ModelLocator from Part 1

If you need information about the ModelLocator, please return to Part 1 of the tutorial.

The only item that must be changed in the ModelLocator is the "package" statement. For this project, you will be placing the ModelLocator in the "model" folder, so the package path simply needs to me "model" (it has already been corrected above). You will also need to add one variable to your ModelLocator initially. This variable will be called "workflowState" and it will be of type "uint". The declaration will look like this:

Actionscript:
  1. public var workflowState:uint = 0;

Example 2 - Defining the workflowState Variable

This variable will be used to "control" the view in your application. The most common way to accomplish this is to use a ViewStack [ Reference ]. If you are not familiar with a ViewStack, feel free to read through this information. A ViewStack has a property named "selectedIndex". This numeric value defines which "child" is visible in the ViewStack. Consider the following code:

mxml:
  1. <mx:ViewStack id="myViewStack">
  2.  
  3.   <mx:HBox id="box1">
  4.     <mx:Label text="I am Box 1" />
  5.   </mx:HBox>
  6.  
  7.   <mx:HBox id="box2">
  8.     <mx:Label text="I am Box 2" />
  9.   </mx:HBox>
  10.  
  11.   <mx:HBox id="box3">
  12.     <mx:Label text="I am Box 3" />
  13.   </mx:HBox>
  14.  
  15. </mx:ViewStack>

Example 3 - A Basic ViewStack Example

Initially the value of selectedIndex is 0. With this setting "box1" would be visible. If you issue the following command:

Actionscript:
  1. myViewStack.selectedIndex = 1;

Example 4 - Manually Setting the selectedIndex

then the box named "box2" would be visible. However, if you apply the ModelLocator to this concept, you could use the workflowState varaible to set the selectedIndex property. By binding the selectedIndex to the workflowState value, you now have complete control over what is displayed in the ViewStack from your ModelLocator.

mxml:
  1. <mx:ViewStack id="myViewStack" selectedIndex="{modelLocator.workflowState}">
  2.   ...
  3. </mx:ViewStack>

Example 5 - Binding the selectedIndex to the workflowState

Defining Constants for Better Code

It would be simple to manipulate the view using this method, however, it could lead to potentially confusing code. For example, assume that you have the following:

  • A ViewStack with two children: a Login Screen and a Welcome Screen
  • The ViewStack's selectedIndex is bound to the workflowState property
  • A login button that performs the action shown in Example 4

This might seem as if it works properly, but it doesn't account for any changes. If another child is added to the ViewStack, it could throw off the order. There needs to be a better way to manually set the selectedIndex property. To accomplish this you just need to define constants inside of the ModelLocator.

Actionscript:
  1. //DEFINE YOUR VARIABLES HERE
  2. public var workflowState:uint = 0;
  3.  
  4. // DEFINE VIEW CONSTANTS
  5. public static const LOGIN_SCREEN = 0;
  6. public static const WELCOME_SCREEN = 1;

Example 6 - Defining View Constants in the ModelLocator

By using this method, you only have to change the value in one location if the number of children or the order of the children changes in the ViewStack. Now, you would assign the login button the following action to the click event:

Actionscript:
  1. myViewStack.selectedIndex = ViewModelLocator.WELCOME_SCREEN;

Example 7 - Setting the View with Defined Constants

Not only do you protect against future changes, you also have made your code much more logical. Another developer could easily look at the code and understand the process without having to reference all of the children in the ViewStack.

NOTE: The audio was not that great for this video. I will be using a better system for the next tutorial.

Video Example - Controlling the View with a ModelLocator

Application Code
Download (3 kB)

Cairngorm, Flex | Comments | Trackback | Del.icio.us | Digg | Technorati Jump to the top of this page

12 comments on “Getting Started with Cairngorm – Part 2”

  1. 01

    These tutorials are very helpful coming from a person who has a somewhat limited programming background. I have done a lot of searching online and find your material to be the most easily understood thus far… can’t wait for part 3.

    Flex New Guy at October 25th, 2007 around 4:19 pm
    Jump to the top of this page
  2. 02

    Hi from Finland!
    Thanks for creating these tutorials! helps me alot! Keep up good work! Whats gonna be in next part?

    Erno at October 28th, 2007 around 7:43 am
    Jump to the top of this page
  3. 03

    I am working on Part 3 right now. Part 3 is the Introduction to the full Cairngorm Micro-Architecture. It will cover the Cairngorm Event Flow and the organization of a Cairngorm project.

    David Tucker at October 28th, 2007 around 7:46 am
    Jump to the top of this page
  4. 04

    [...] Teil 2 dreht sich um die Implementierung des Views mittels des ModelLocator Patterns [...]

    Jump to the top of this page
  5. 05

    Hi David,

    May I download your flv file.the movie is very useful for me.Thanks

    Tony Chang at November 6th, 2007 around 9:11 am
    Jump to the top of this page
  6. 06

    [...] Part 2 [...]

    THE EYE OF RIA » Getting Started with Cairngorm at November 29th, 2007 around 7:19 pm
    Jump to the top of this page
  7. 07

    [...] Part 2 [...]

    Jump to the top of this page
  8. Jump to the top of this page
  9. 09

    [...] Tradução do original inglês: http://www.davidtucker.net [...]

    Jump to the top of this page
  10. 10

    Hi,I’m from China.
    These tutorials are greet && thanks you!

    Dekki at March 4th, 2008 around 7:17 pm
    Jump to the top of this page
  11. 11

    Hi,

    I’m from South Africa and I have been battling with the framework until I stumbled on this tutorials. Thanks to you i now have my view managers setup. Now I’m off to Part 3!! You are great teacher & trainer.

    Azwidohwi at March 15th, 2008 around 4:55 am
    Jump to the top of this page
  12. 12

    Great stuff. Thanks for taking the time to publish it.

    Rob at September 1st, 2008 around 6:40 pm
    Jump to the top of this page

Leave a Reply

  •  
  •  
  •  

You can keep track of new comments to this post with the comments feed.

Badges

View David Tucker's profile on LinkedIn
Inside RIA Badge

Community Posts