in a rush

experience is everything

in a rush header image 2

Dynamically adding to a Model

April 28th, 2008 · No Comments

One limitation of the Model tag is that if you supply one instance of a child tag in an <mx:Model> tag, there is no way for Flex to know if you intend it to be an array or a single property instance. You can work around this limitation by using an <mx:Object> tag instead of an <mx:Model> tag and declaring an <mx:Array> tag inside the <mx:Object> tag. As an added benefit, you can then dynamically add to the object creating an array of objects.

Here’’s the sample code:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*">
  3.  
  4. <mx:Script>
  5. <![CDATA[
  6. public var count:Number = 0;
  7.  
  8. public function addToModel():Void
  9. {
  10. var newRecord:Object = new Object();
  11. newRecord.name = "Toby";
  12. newRecord.sign = "Leo";
  13. newRecord.birthYear = "1984";
  14. test[ count++ ] = newRecord;
  15. }
  16. ]]>
  17. </mx:Script>
  18.  
  19. <mx:Object id="test"/>
  20.  
  21. <mx:Button label="Add" click="addToModel()"/>
  22.  
  23. <Inspect/>
  24.  
  25. </mx:Application>
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati

Tags: Uncategorized

0 responses so far ↓

  • There are no comments yet...Kick things off by filling out the form below.

Leave a Comment