View comments | RSS feed

<mx:Model> tag implemented by the compiler


You use the <mx:Model> tag to declare a data model in MXML. An <mx:Model> tag is compiled into a tree of ActionScript objects; the leaves of the tree are scalar values.

MXML Syntax

You can place an <mx:Model> tag in a Flex application file, or in an MXML component file. The tag must have an id value. It cannot be the root tag of an MXML component. The <mx:Model> tag has the following syntax:

 <mx:Model id="modelID">
model declaration
</mx:Model>

or:

 <mx:Model id="modelID" source="fileName" />

where source specifies an external source, such as a file, for the data model. The external source can contain static data and data binding expressions. The file referenced in a source property resides on the server and not on the client machine. The compiler reads the source value and compiles the source into the application; the source value is not read at runtime.

The model declaration, either in-line in the tag or in the source file, must have a single root node that contains all other nodes. You can use MXML binding expressions, such as {myForm.lastName.text} in the model declaration. This way you can bind the contents of form fields to a structured data representation.

In the following example, the myEmployee model is placed in an MXML application file:

	<?xml version="1.0"?>
	<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
	...
	  <mx:Model id="MyEmployee">
	    <root>
	        <name>
	           <first>Will</first>
	           <last>Tuckerman</last>
	        </name>
	        <department>Accounting</department>
	        <email>wtuckerman@wilsoncompany.com</email>
	    </root>
	  </mx:Model>
	...
	</mx:Application>


Comments


mpeterson said on Jul 6, 2006 at 8:51 AM :
The following text from this page is misleading:
"The file referenced in a source property resides on the server and not on the client machine."
A file referenced in the source property of an mx:Model tag is actually read and compiled into the application at compile time. When using the commandline compiler, the file would be local. When using the web tier compiler, the file would be on a server, but it is not read at run time.

- Mike Peterson
Adobe Flex documentation team
mpeterson said on Aug 29, 2006 at 2:57 PM :
The mx:Model tag is compiled into an object of type mx.utils.ObjectProxy.

- Mike Peterson
Flex doc team
Rambo 007 said on Jan 10, 2007 at 12:10 PM :
The examples that I can find in the docs seem to be sketchy and buggy at best. I *think* that it's the case that, when referring to members of the model, the "root" element doesn't count, so for

<mx:Model id="foo">
<root>
<aString>foo</aString>
</root>
</mx:Model

If I wanted to refer to aString in a binding expression, I'd say "{foo.aString}" and not "{foo.root.aString}", correct? This point needs to be clarified, and it would be a good thing to parenthetically note that requiring a root element is a non-compatible change with Flex 1.5.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flex/2/langref/mxml/model.html