views:

181

answers:

2

I've found it's often useful to special case the first item in a drop-down menu (ie, an instance of Menu). For example, if I want to pick a color from the list provided by a web service:

<mx:PopUpMenuButton id="colorSelelector"
    dataProvider="{colorsService.lastResult}" />

I might also want a special-case, which is "enter a new color", allowing the user to enter the RGB values for a new color which isn't in the list. For example:

var newColor = { label: "Enter a new color", rgb: null };

Then used with:

<mx:PopUpMenuButton id="colorSelelector"
    dataProvider="{colorsService.lastResult}"
    lastOption="{newColor}" />

So, apart from changing the list I get back from the service, is there any better way to do this?

(and just a preemptive comment: this is a simplification… I'm not actually trying to make a color-picking-list)

+1  A: 

When you bind to the dataProvider, call a function that adds your special case. For instance:

<mx:PopUpMenuButton id="colorSelector" 
    dataProvider="{addSpecialCases(colorsService.lastResult)}"/>
Dan Monego
+1  A: 

So, apart from changing the list I get back from the service, is there any better way to do this?

This approach is going to be the cleanest, without extending HTTPService, which would work well (but is really just altering your result ;) ):

package
{
    import mx.rpc.http.HTTPService;

    public class MyHTTPService extends HTTPService
    {
     public var appendToResult:Object;

     public function MyHTTPService(rootURL:String=null, destination:String=null)
     {
      super(rootURL, destination);
     }

        [Bindable("resultForBinding")]
        override public function get lastResult():Object
        {
         //I know what my type is, Array as an example
         var myResult:Array = operation.lastResult;
         myResult.push( this.appendToResult )
            return myResult;
        }
    }
}
Joel Hooks