19.5. Command Classes

You need command classes for both loading and adding comments.

Start with the command for loading comments. You need a property on the model to represent the comments loaded by the command class. Add the following to the FlexBlogModel:

public var comments:ArrayCollection =new ArrayCollection();

Now create the command class. In the com.FlexBlog.commands package create a new command class named LoadCommentsCommand. Edit the class to match the following:

package com.FlexBlog.commands
{
    import com.FlexBlog.delegates.LoadCommentsDelegate;
    import com.FlexBlog.events.LoadCommentsEvent;
    import com.FlexBlog.models.FlexBlogModel;
    import com.adobe.cairngorm.commands.ICommand;
    import com.adobe.cairngorm.control.CairngormEvent;
    import mx.rpc.IResponder;
    import mx.rpc.events.ResultEvent;
    public class LoadCommentsCommand implements ICommand, IResponder
    {
        private var model:FlexBlogModel = FlexBlogModel.getInstance();
        public function execute(event:CairngormEvent):void
        {
            var evt:LoadCommentsEvent = event as LoadCommentsEvent;
            var delegate:LoadCommentsDelegate = new LoadCommentsDelegate(this);
            delegate.loadComments(evt.post);
        }
        public function result(data:Object):void
        {
            var evt:ResultEvent = data as ResultEvent;
            this.model.comments.source = evt.result.comments;
        }
        public function fault(info:Object):void
        {
        }
    }
}

The execute function calls the loadComments function of the LoadCommentsDelegate, passing it the PostVO object from the LoadCommentsEvent event.

Using the comments property ...

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.