ActionScript , Flex

Encapsulating RemoteObject in Flex 4

April 22, 2010

With the ease of customization of Flex, you can expedite the production of their applications by encapsulating internal routines (the player) or external, by creating a custom component that will be used for any application.

Here I board the DataGrid, but with the simplicity of ActionScript, you realize that you will can easily change it to other components like ComboBox, List, etc ...

RemoteDataGrid.as

package view.components
{
    import mx.collections.ArrayCollection;
    import mx.controls.Alert;
    import mx.controls.DataGrid;
    import mx.events.DataGridEvent;
    import mx.events.FlexEvent;
    import mx.rpc.Fault;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.remoting.Operation;
    import mx.rpc.remoting.RemoteObject;
   
    //use as the basis of the component, the DataGrid Class
    public class RemoteDataGrid extends DataGrid
    {
       
        //property that contain access AMF
        private var _remoteObject:RemoteObject;
        //propriedade que para lock de execução remota
        private var _inUse:Boolean = false;
       
        //property to configures access AMF
        public var remoteDestination:String;
        public var remoteSource:String;
        public var remoteShowBusyCursor:Boolean = true;
       
        //property that contains the name of the remote method
        public var remoteOperation:String;
        public var remoteSortOperation:String;
       
        //properties will be used in paging data
        public var remoteCount:Number = 10;
        public var remoteStart:Number = 1 ;
        public var remoteFilter:Object = new Object();
       
       
       
        public function RemoteDataGrid()
        {
            //add action to create the component which is configured remote access
            addEventListener(FlexEvent.CREATION_COMPLETE,internalStart);
            super();
        }
       
        private function internalStart(event:FlexEvent):void
        {
            if(remoteSource != null && remoteDestination != null){
                _remoteObject = new RemoteObject();
                _remoteObject.destination = remoteDestination;
                _remoteObject.source = remoteSource;
                _remoteObject.showBusyCursor = remoteShowBusyCursor;
                _remoteObject.addEventListener(ResultEvent.RESULT,gridResultHandler);
                _remoteObject.addEventListener(FaultEvent.FAULT,gridFaultHandler);
                if(remoteSortOperation != null){
                    //add action to sort the columns in the database when clicked
                    addEventListener(DataGridEvent.HEADER_RELEASE,geridHeaderHandler);
                }
                if(remoteOperation != null){
                    //recover method that will be executed immediately of the create the component
                    var currentOperation:Operation = _remoteObject.getOperation(remoteOperation) as Operation;
                    if(_inUse == false){
                        _inUse = true;
                        currentOperation.send(remoteStart,remoteCount,remoteFilter);
                    } else{
                        Alert.show('Is busy','Waiting');
                    }
                }
            }
           
        }
       
        //action of default return
        private function gridResultHandler(event:ResultEvent):void
        {
            var currentData:ArrayCollection = new ArrayCollection();
            currentData.source = event.result as Array;
            dataProvider = currentData;
            _inUse = false;
        }
       
        //action of default error
        private function gridFaultHandler(event:FaultEvent):void
        {
            Alert.show(event.fault.message.toString(),event.fault.faultCode.toString());
            _inUse = false;
        }
       
        //action of default sort
        private function geridHeaderHandler(event:DataGridEvent):void
        {
            if(!_inUse){
                _inUse = true;
                var orderBy:String = event.dataField+' asc';
                //recover sort method to Grid
                var currentOperation:Operation = _remoteObject.getOperation(remoteSortOperation) as Operation;
                /*
                the remote method should expect parameters:
                remoteStart - initial row of the paging
                remoteCount - amount of records of the page
                remoteFilter - criterion for filter must contain pairs of ColumnName=Value
                orderBy - internal
               
                */
                currentOperation.send(remoteStart,remoteCount,remoteFilter, orderBy);
            } else{
                Alert.show('Is busy','Waiting');
            }
            event.preventDefault();
        }
       
    }
}

And his call in the application shall be as follows:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               xmlns:view="view.components.*">
    <view:RemoteDataGrid
            remoteDestination="ColdFusion"
            remoteOperation="getArtists"
            remoteSortOperation="getArtists"
            remoteSource="com.pcsilva.blog.blog21042010.cfml.dao.ArtistsDAO" />
</s:Application>

Example



Send

Share your view

Add Comment

Subscribe

Enter your email address to subscribe to this blog.

Digitrum Servers Powered by Blogcfc 5.9.2.002