Dynamically adding to a Model

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:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*">

<mx:Script>
<![CDATA[
public var count:Number = 0;

public function addToModel():Void
{
var newRecord:Object = new Object();
newRecord.name = "Toby";
newRecord.sign = "Leo";
newRecord.birthYear = "1984";
test[ count++ ] = newRecord;
}
]]>
</mx:Script>

<mx:Object id="test"/>

<mx:Button label="Add" click="addToModel()"/>

<Inspect/>

</mx:Application>
Pass it on:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • LinkedIn
  • MySpace
  • TwitThis
  • Google Bookmarks
  • Live-MSN
  • Pownce
This entry was posted in Flash Platform, Flex and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>