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="*">

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:Object id="test"/>

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



About Rob Rusher

In his role as Principal Consultant for On3, Rob leads an software development practice to help his clients build rich Internet applications for the desktop, browser, and mobile devices, and to rapidly increase their knowledge and skills to better support their organization's goals. Rob is an Adobe Certified Expert, Community Professional, and Certified Instructor. He has taught and mentored the technical teams at HP, Overstock, Paychex, SAS, the FedEx, and other Government and Fortune 100 organizations. Rob has co-authored four best-selling books on building secure, cutting-edge and rapidly developed applications using Adobe AIR, ColdFusion and Flex. He is very active in organizing and speaking at RIA, Adobe LiveCycle, mobile conferences, and user groups. In addition to growing his software consulting practice, On3, Rob has been building expertise in rich client application development on a wider variety of devices and platforms that extend the applications to change the way we all create and live.