4.4. How Do You Use It?

Once you have your ModelLocator class created, the next step is to add public properties representing the data that needs to be accessed to the ModelLocator. As mentioned previously, in many cases this data consists either of one of the Flex mx.collections classes or instances of value object classes, as can be seen in the following:

package com.cairngormexample.model
{
    import mx.collections.ArrayCollection;
    import com.someproject.vo.UserVO;

    public class CairngormExampleModel
    {
        private static var instance:CairngormExampleModel;
        public var user:UserVO;
        public var items:ArrayCollection=new ArrayCollection();
        public var purchasedItems:ArrayCollection=new ArrayCollection();
        public function CairngormExampleModel(se:SingletonEnforcer)
        {
        }
        public static function getInstance():CairngormExampleModel{
            if (CairngormExampleModel.instance == null){
                CairngormExampleModel.instance = new CairngormExampleModel(new
SingletonEnforcer());
            }
            return CairngormExampleModel.instance;
        }
    }
}
class SingletonEnforcer{};

Once these properties have been added you simply need to get a reference to the ModelLocator and then reference the properties, as in the following:

var model: CairngormExampleModel = CairngormExampleModel.getInstance();
//user
model.user;
//items
model.items
//purchased items
model.purchasedItems

Once you have this reference you can use the properties of the ModelLocator for data binding in components. The following example is again from the Cairngorm store application. ...

Get Professional Cairngorm™ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.