package custom { import flash.events.Event; import flash.net.SharedObject; import mx.controls.TextInput; publicclass PersistentTextInput extends TextInput { /** * The ID this component will use to save and later look up its * associated value. */ publicvar persistenceId:String = null; /** * The SharedObject name to use for storing values. */ privatestaticconst LOCAL_STORAGE_NAME:String = "persistentTextInputStorage"; /** * Clears previously stored values for all PersistentTextInput instances. */ publicstaticfunction clearStoredValues() :void { var so:SharedObject = SharedObject.getLocal(LOCAL_STORAGE_NAME); so.clear(); } /** * Handles initialization of this component. */ override publicfunction initialize() :void { super.initialize(); addEventListener(Event.CHANGE, handleChange); restoreSavedValue(); } /** * Event handler function for CHANGE events from this instance. */ protectedfunction handleChange(event:Event) :void { saveCurrentValue(); } /** * Restores the previously saved value associated with the * persistenceID of with this instance. */ protectedfunction restoreSavedValue() :void { if (persistenceId != null) { var so:SharedObject = SharedObject.getLocal(LOCAL_STORAGE_NAME); var value:String = so.data[persistenceId]; if (value != null) { text = value; } } } /** * Saves the text value of this instance. Associates the value with * the persistenceId of this instance. */ protectedfunction saveCurrentValue() :void { if (persistenceId != null) { var so:SharedObject = SharedObject.getLocal(LOCAL_STORAGE_NAME); so.data[persistenceId] = text; so.flush(); } } } }
<mx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml" xmlns:custom="custom.*" layout="vertical"> <mx:Script> <![CDATA[ import custom.PersistentTextInput; publicfunction clearValues() :void { PersistentTextInput.clearStoredValues(); message.text = "A page refresh will reveal that the values have not persisted."; }