15.5. Command Classes

Since you will be changing main views by binding to a variable on the ModelLocator, you need to add that variable to the model. Add the following to the FlexBlogModel:

public var mainView:int;

Now you need a command class to update this variable. In the com.FlexBlog.commands package create a new command class named ChangeMainViewCommand and edit to match the following:

package com.FlexBlog.commands
{
    import com.FlexBlog.events.ChangeMainViewEvent;
    import com.FlexBlog.models.FlexBlogModel;
    import com.adobe.cairngorm.commands.ICommand;
    import com.adobe.cairngorm.control.CairngormEvent;
    public class ChangeMainViewCommand implements ICommand
    {
        private var model:FlexBlogModel = FlexBlogModel.getInstance();
        public function execute(event:CairngormEvent):void
        {
            var evt:ChangeMainViewEvent = event as ChangeMainViewEvent;
            this.model.mainView = evt.goTo;
        }
    }
}

This command updates the mainView property of the ModelLocator.

To return for a moment to the idea of dealing with change (e.g., changing the main view to be something other than a ViewStack), you do not even have to touch the command class, as all it does is update the model with the value passed in the goTo property of the event. As long as you have updated the event and the model to expect the same type of parameter, you are good to go.

Now register the ChangeMainViewEvent and ChangeMainViewCommand in the FlexBlogController:

public function FlexBlogController() { super(); addCommand(ChangeMainViewEvent.CHANGE_MAIN_VIEW, ...

Get Professional Cairngorm™ now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.