18.5. Command Classes

You need command classes for application initialization, the loading of the posts, and setting the current post.

Start with the application initialization command. In the com.FlexBlog.commands package create a new command class named ApplicationInitializeCommand. Edit it to match the following:

package com.FlexBlog.commands
{
    import com.FlexBlog.events.LoadCategoriesEvent;
    import com.FlexBlog.events.LoadRecentPostsEvent;
import com.adobe.cairngorm.commands.ICommand;
    import com.adobe.cairngorm.control.CairngormEvent;
    public class ApplicationInitializeCommand implements ICommand
    {
        public function execute(event:CairngormEvent):void
        {
            new LoadCategoriesEvent().dispatch();
            new LoadRecentPostsEvent().dispatch();
        }
    }
}

Note that the execute function is dispatching the events for anything that needs to occur when the application loads. Thus far this includes loading the categories and recent posts. From now on, when anything needs to happen when the application first loads, you can add it to this command class and not have to worry about it in your main view.

Next move on to the command class for loading posts. You need a property on the ModelLocator to represent the list of recent posts in addition to the property representing the currently selected post. Add the following to the FlexBlogModel:

public var recentPosts:ArrayCollection = new ArrayCollection();
public var currentPost:PostVO;

Make sure Flex has added the import statements for you.

Now create the command ...

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.