The ModelLocator Holds Application Data

(Go to 'Cairngorm Series' to view all titles)

This is the second post in a series on Cairngorm fundamentals. We will be covering the concept of the ModelLocator, the Model in MVC. In short, the ModelLocator is a Singleton which implements a marker interface (no method definitions) from the Cairngorm framework called com.adobe.cairngorm.model.IModelLocator. The ModelLocator class defines properties that hold all application data. From now on we'll just call this class the Model.

The ModelLocator Holds Application Data

Get the Flash Player to see this player.

The source for the ExamplModelLocator file created in the video is shown below with comments. If you have not watched the video, please do so for a full explanation of the code.

Actionscript:
  1. package com.tsclausing.model
  2. {
  3.     // import IModelLocator marker interface from the Cairngorm framework
  4.     //
  5.     import com.adobe.cairngorm.model.IModelLocator;
  6.    
  7.      // public properties of the ExampleModelLocator will be bindable in to any View.
  8.      //
  9.     [Bindable]
  10.     public class ExampleModelLocator implements IModelLocator
  11.     {
  12.         // the value we will be incrementing in this example application
  13.         //
  14.         public var value : Number = 0;
  15.        
  16.         // other public properties would be placed here
  17.         // ...
  18.        
  19.        
  20.         // the one and only instance of ExampleModelLocator
  21.         //
  22.         private static var instance : ExampleModelLocator;
  23.        
  24.         // return a reference to the one and only instance of ExampleModelLocator
  25.         //
  26.         public static function getInstance ():ExampleModelLocator
  27.         {
  28.             if (!instance) instance = new ExampleModelLocator(new SingletonEnforcer);
  29.             return instance;
  30.         }
  31.        
  32.         // attempt to enforce the singleton implementation
  33.         //
  34.         public function ExampleModelLocator(enforcer:SingletonEnforcer)
  35.         {
  36.             if (instance != null) throw new Error("Singleton Exception");
  37.         }
  38.     }
  39. }
  40. class SingletonEnforcer {}

Where we are headed: MVC

Temporarily scratch the Model off the list. We'll improve upon the way we store application data later, but we now have one and only one place to store data that is accessible through data binding in any View. Still to come: the View(s) and Controller. The View for this example 'value incrementing application' will be created and bound to the Model in the next video.

(Go to 'Cairngorm Series' to view all titles)

1 Response to “The ModelLocator Holds Application Data”


  1. 1 Johan

    Nice tutorial and explanation of singleton/modelocator. Thanks!

Leave a Reply