20.4. Delegate Classes

Both types of searches require interaction with the database, so you will need delegate classes for both.

Start with the delegate class for the keyword search. In the com.FlexBlog.delegates package create a new delegate class named KeyWordSearchDelegate. 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 KeyWordSearchDelegate
    {
        private var responder:IResponder
        public function KeyWordSearchDelegate(responder:IResponder)
        {
            this.responder = responder;
        }
        public function search(keyWords:String):void{
            var dbManager:FlexBlogDatabaseManager =
FlexBlogDatabaseManager.getInstance();
            try{
            var posts:Array =  dbManager.keyWordSearch(keyWords);
            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 search function takes in a string of keywords and calls the keyWordSearch function of the FlexBlogDatabaseManager, passing it the keywords, which returns an array of PostVO objects. This array is passed back to the command class in the posts property of the result object of the ResultEvent.

If something goes wrong during the search, the fault function of the responder is called.

Next, create the delegate ...

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.