8.4. How Do You Use Them?

Delegates are used in command classes when they need to access a remote service. An example of this can be seen in the GetProductsCommand from the Cairngorm web store:

package com.adobe.cairngorm.samples.store.command
{
    import mx.rpc.IResponder;
    import com.adobe.cairngorm.commands.ICommand;
    import com.adobe.cairngorm.control.CairngormEvent;
    import com.adobe.cairngorm.samples.store.business.ProductDelegate;
    import com.adobe.cairngorm.samples.store.model.ShopModelLocator;
    import com.adobe.cairngorm.samples.store.util.Comparator;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.events.FaultEvent;
    import mx.controls.Alert;
    import mx.collections.ICollectionView;
    import mx.collections.Sort;
    import mx.collections.SortField;
    import mx.utils.ArrayUtil;
    public class GetProductsCommand implements ICommand, IResponder
    {
        public function GetProductsCommand()
        {
        }
        public function execute( event : CairngormEvent ): void
        {
            if( ShopModelLocator.getInstance().products == null )
            {
                var delegate : ProductDelegate = new ProductDelegate( this );
                delegate.getProducts();
            }
            else
            {
            Alert.show( "Products already retrieved!" );
return;
            }
        }
        public function result( event : Object ) : void
        {
            var products : ICollectionView = ICollectionView( event.result );
            var model : ShopModelLocator = ShopModelLocator.getInstance();
            // sort the data
            var sort :Sort = new Sort();
            sort.fields = [ new SortField( "name", true ) ];
            products.sort = sort;
            products.refresh();
        // set the products on the model ...

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.