简体   繁体   中英

Flex: getting the previous value of a combobox when it changes

I need to know the value from which a combo box is changing, when it changes. I've been all through the documentation for the events, and none of them let you know what the value is before user interaction changes it. ( currentStateChanging is a total red herring!) Handling the open event and saving the value isn't a solution, because there are other ways to change the value.

I'm using the Flex 3.5 SDK.

Something like this?

var currentVal : Object;

private function onChange(newVal) : void {
    // currentVal stores your previous value - do something with it
    currentVal = newVal;
}

<mx:ComboBox change="onChange(event.target.selectedItem)"/>

I just used the "changing" event on a Spark ComboBox to solve this very problem but it's not available on the mx version

Also - see this

I've come up w/a solution, but it's not perfectly reliable (since it makes assumptions about how it will work in other SDKs) and its elegance is wanting:

<mx:ComboBox    xmlns:mx="http://www.adobe.com/2006/mxml" valueCommit="OnChangeAnyway()" change="OnChange()">
<mx:Metadata>
    [Event(name='traceable_change', type="assets.LineComboChangeEvent")]
</mx:Metadata>

<mx:Script><![CDATA[
    public static const CHANGE:String = 'traceable_change';

    private var m_oOld:Object;
    private var m_oNew:Object;
    private var m_bCallLaterPending:Boolean = false;  //This is necessary, because I found OnChangeAnyway() could be called any number of times before OnChange() is

    private function OnChange():void {
        var oNewEvent:LineComboChangeEvent = new LineComboChangeEvent(CHANGE, m_oOld);   //there's nothing special in this class
        dispatchEvent(oNewEvent);
    }

    private function OnChangeAnyway():void {
        if (!m_bCallLaterPending) {
            m_bCallLaterPending = true;
            callLater(function ():void { m_bCallLaterPending = false;}, []);   //presumably, whatever is passed to callLater() will be executed after everything else currently queued
            m_oOld = m_oNew;
            m_oNew = value;
        }
    }
]]></mx:Script>

m_oNew is obviously redundant because that value will be available to whatever handles traceable_change , but it does explain why I have to barrel-shift these objects. There are a couple of reasons why I don't consider this reliable:

  1. It assumes that the valueCommit handler will be called ahead of the change one. On my system, it always seems to, but I don't see that promise anywhere.
  2. It assumes that whatever callLater() calls will be called after change is. See concerns for 1.

I've come to the conclusion that there isn't an answer :( The best workaround is to override all possible ways there are to set the value of a combo box, plus handle any events that involve the user changing the value, back up that value and then you have a trail of previous values. Then, put a lot of comments saying

this is a 3.5-necessary kluge! If doing this on another SDK you might have to change it!

=======

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM