19.4. Delegate Classes

Both the loading and the adding of comments require interaction with the database, so you will need delegate classes for both procedures.

Start with the delegate for loading comments. In the com.FlexBlog.delegates package create a new delegate class named LoadCommentsDelegate. Edit the class to match the following:

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

The loadComments function calls the getComments function of the FlexBlogDatabaseManager, which returns an array of CommentVO objects. This array is passed back to the command class in the comments property of the result object of the ResultEvent.

If something goes wrong while the comments are being retrieved, the fault function of the ...

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.