O'Reilly logo

Professional Cairngorm™ by Jeremy Wischusen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

14.2. Creating the ModelLocator

The ModelLocator class, unlike the FrontController and the ServiceLocator, is not required to be in the main application file in order for other classes to work, but you will need it for many of the classes that you will create later.

Create a class named FlexBlogModel in the com.FlexBlog.models package. (See Chapter 4 for specific instructions with screenshots.) Edit the class to look as follows:

package com.FlexBlog.models
{
    [Bindable]
    public class FlexBlogModel
    {
        private static var instance:FlexBlogModel = null;
        public function FlexBlogModel(se:SingletonEnforcer)
        {
        }
        public static function getInstance():FlexBlogModel{
          if (FlexBlogModel.instance == null){
              FlexBlogModel.instance = new FlexBlogModel(new SingletonEnforcer());
            }
            return FlexBlogModel.instance;
        }
    }
}
class SingletonEnforcer{};

Make sure that you have added the Bindable metatag before the class definition. This allows other classes to bind to properties on the model.

Of all the singletons used in the Cairngorm framework, the ModelLocator is the only one that you need to implement as a singleton on your own.

For the singleton implementation, make sure that you have:

  • Created a private static variable named instance that will hold the single instance of the class

  • Created an internal class named SingletonEnforcer outside of the package definition

  • Created the constructor method to take in a parameter of type SingletonEnforcer

  • Created a public static method named getInstance that creates the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required