I wrote some sample code to try to help me figure out how to properly
create and use a global instance of data throughout my Flex
application. The idea was to create an actionscript class that
contains a static instance of an Array / DataProvider, initialize it
on creation of the application, and then bind multiple components to
it, so they would all update when a modelChanged event was broadcast.
With debug printouts or using an object inspector, I'm seeing some
really strange behavior with this sample application. If I add an
item to the global array, it shows up on the two lists, but doesn't
actually appear in the array, and the array size doesn't get any
bigger. If I delete an item, the item gets deleted from both lists
and the array, but again, the array.length property remains unchanged.
Am I just missing something simple here about how to do this? My two
source files and xml data are pasted here if you'd like to try them
out and see if you can spot what it is that I'm doing wrong.
Thanks for any assistance!
-Tom Jeffery
--- GlobalModel.as ---
import mx.controls.listclasses.DataProvider;
class GlobalModel {
static var globalModel:DataProvider;
}
--- data.xml ---
<data>
<item>
<label>Test Item 1</label>
</item>
<item>
<label>Test Item 2</label>
</item>
<item>
<label>Test Item 3</label>
</item>
<item>
<label>Test Item 4</label>
</item>
</data>
--- DataTest.mxml ---
<?xml version="1.0" ?>
<mx:Application xmlns="*"
xmlns:mx="http://www.macromedia.com/2003/mxml" width="100%"
height="100%"
xmlns:local="*" creationComplete="doInit();" >
<!-- <local:Inspect/>-->
<mx:Script>
<![CDATA[
import GlobalModel;
import mx.controls.listclasses.DataProvider;
function addText():Void {
//testModel.item.addItem(testInput.text);
GlobalModel.globalModel.addItem(testInput.text);
//testList1.addItem(testInput.text);
}
function deleteItem():Void {
if (testList1.selectedIndex == undefined)
return;
GlobalModel.globalModel.removeItemAt(t
estList1.selectedIndex);
}
function doInit():Void {
GlobalModel.globalModel = testModel.item;
}
]]>
</mx:Script>
<mx:Model id="testModel" source="./data.xml"/>
<mx:VBox>
<mx:HBox>
<mx:List id="testList1" dataProvider="{testModel.item}"
multipleSelection="false" />
<mx:List id="testList2"
dataProvider="{GlobalModel.globalModel}"
selectable="false" />
</mx:HBox>
<mx:HBox>
<mx:TextInput id="testInput" />
<mx:Button id="testButton" click="addText();"
label="Add To Model
{GlobalModel.globalModel.length}" />
<mx:Button click="deleteItem()" label="Delete Selected"/>
</mx:HBox>
</mx:VBox>
</mx:Application>
--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/flexcoders/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/