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

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

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