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

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.

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