19.7. Implementation and Testing

Since you want comments to be loaded whenever a post is selected, you need to trigger the LoadCommentsEvent whenever this occurs. Since you already have a command class for setting the current posts, you can dispatch the LoadCommentsEvent there. Edit the execute function of the SetCurrentPostCommand to match the following:

public function execute(event:CairngormEvent):void
        {
            var evt:SetCurrentPostEvent = event as SetCurrentPostEvent;
            this.model.currentPost = evt.post;
            new LoadCommentsEvent(this.model.currentPost).dispatch();
        }

Make sure that Flex has added the import statements for you.

Now, whenever a post is selected, the associated comments will be loaded as well. However, you'll recall that when you created the LoadRecentPostsCommand, you directly set the currentPost property of the ModelLocator. This would not cause the LoadCommentsEvent in the SetCurrentPostEvent to be triggered. Having the LoadRecentPostsCommand dispatch a SetCurrentPostEvent rather than directly updating the ModelLocator can fix this. Edit the result function of the LoadRecentPostsCommand to match the following:

public function result(data:Object):void
        {
            var evt:ResultEvent =  data as ResultEvent;
            if (evt.result.posts){
                this.model.recentPosts.source = evt.result.posts;
                if(!this.model.currentPost){
                    new
SetCurrentPostEvent(PostVO(this.model.recentPosts.getItemAt(0))).dispatch();
                }
            }
        }

Make sure that Flex has added the import statements for you.

Now that the SetCurrentPostEvent ...

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.