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

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

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