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

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

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