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

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 ...

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