Flex: Special-casing an item in a list or menu?

2019-08-21 09:59发布

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)

2条回答
混吃等死
2楼-- · 2019-08-21 10:16

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

<mx:PopUpMenuButton id="colorSelector" 
    dataProvider="{addSpecialCases(colorsService.lastResult)}"/>
查看更多
beautiful°
3楼-- · 2019-08-21 10:18

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;
        }
    }
}
查看更多
登录 后发表回答