(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.
-
package com.tsclausing.model
-
{
-
// import IModelLocator marker interface from the Cairngorm framework
-
//
-
import com.adobe.cairngorm.model.IModelLocator;
-
-
// public properties of the ExampleModelLocator will be bindable in to any View.
-
//
-
[Bindable]
-
public class ExampleModelLocator implements IModelLocator
-
{
-
// the value we will be incrementing in this example application
-
//
-
public var value : Number = 0;
-
-
// other public properties would be placed here
-
// ...
-
-
-
// the one and only instance of ExampleModelLocator
-
//
-
private static var instance : ExampleModelLocator;
-
-
// return a reference to the one and only instance of ExampleModelLocator
-
//
-
public static function getInstance ():ExampleModelLocator
-
{
-
if (!instance) instance = new ExampleModelLocator(new SingletonEnforcer);
-
return instance;
-
}
-
-
// attempt to enforce the singleton implementation
-
//
-
public function ExampleModelLocator(enforcer:SingletonEnforcer)
-
{
-
if (instance != null) throw new Error("Singleton Exception");
-
}
-
}
-
}
-
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)
Nice tutorial and explanation of singleton/modelocator. Thanks!