18.4. Delegate Classes

Only the loading of the posts needs to interact with the database, so you need only a delegate for that process.

In the com.FlexBlog.delegates package, create a new delegate class named LoadRecentPostsDelegate. Edit the class to match the following:

package com.FlexBlog.delegates
{
    import flash.errors.SQLError;
    import mx.rpc.IResponder;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
    import sql.FlexBlogDatabaseManager;
    public class LoadRecentPostsDelegate
    {
        private var responder:IResponder
        public function LoadRecentPostsDelegate(responder:IResponder)
        {
            this.responder = responder;
        }
        public function loadRecentPosts():void{
            var dbManager:FlexBlogDatabaseManager =
FlexBlogDatabaseManager.getInstance();
            try{
                var posts:Array = dbManager.getRecentPosts();
                var event:ResultEvent = new
ResultEvent(ResultEvent.RESULT,false,true,{posts:posts})
                this.responder.result(event)
            }catch(error:SQLError){
                var faultEvent:FaultEvent = new FaultEvent(FaultEvent.FAULT);
                this.responder.fault(faultEvent);
            }
        }
    }
}

The loadRecentPosts method calls the getRecentPosts method of the FlexBlogDatabaseManager. The getRecentPosts method returns an array of PostVO objects. This array is passed back to the command class using a variable called posts on the result object of the ResultEvent.

If something goes wrong with loading the posts, the fault method of the responder is called.

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.