O'Reilly logo

Learning Flex 4 by Elijah Robison, Alaric Cole

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Handling Complex Data with Data Models

A data model is a single object with multiple properties you can use to store a lot of related information in one place. Flex makes it easy to store structured information in data models, and as we will see, data binding gives us a useful mechanism to benefit from this flexible component.

Building the Data Model

Data models can help you organize your code, and they can be quite practical. For instance, when you’re pulling data from a server, it’s good practice to grab lumps of similar data, such as a person’s name, email, and address, and then store the result in a model for later use.

Example 8-13 demonstrates the data model. Do you see something new? The data model must be declared within a <fx:Declarations/> block, which we’ve been prone to deleting since we haven’t been using it.

Example 8-13. A data model for storing user information

<fx:Declarations>
    <fx:Model id="contact">
        <info>
            <name>
                <first>John</first>
                <last>Doe</last>
                <full>{contact.name.first} {contact.name.last}</full>
            </name>
            <email>john.doe@foo.com</email>
            <phone>555-555-555</phone>
        </info>
    </fx:Model>
</fx:Declarations>

This model allows us to store user information in a central location that’s easy to read and understand. Notice the root of the XML structure is a tag called <info/>. A root tag must be included to create valid XML, but you can call the root anything you want; the word “info” isn’t a requirement.

The beauty of a data model is that it accesses the model’s information ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required