Main

December 14, 2005

Flex 2 Example (admin for news ticker) - Editing XML and saving it back to the server

In this example we pull some XML data into Flex and keep it as an XML object. We then edit the data with a Flex GUI and save it back to the server. To make things more interesting I created a simple “News Ticker” in Flash 8 that is built from the same XML. You get a semi-automatic preview of the ticker directly within the GUI.

I can see a real use for these simple Flex apps to easily allow people to manage all sorts of configurations settings without having to actually look at the XML (or other underlining data).

The ticker (4kb) is shown at the end of this post. Everyone can modify the data using the admin control… so I don’t know how the ticker will look when you view it at this point in time.

There is nothing “fancy” in the Flex code just a lot of semi-manual binding of various data elements to the nodes and attributes in the XML file. The source code is commented and it shouldn’t be hard to understand what is happening. If you have any questions post them below and I’ll answer them.

Files:
Admin For Ticker (requires Flash 8.5 player)
Project File
MXML File
Linked Actionscript in MXML
Current XML

Ticker:



November 29, 2005

Flex Data Binding - Nodes or Attributes?

The last example showed one EASY way to drill down and access your data with Flex.

If you noticed, all of the "data" in my XML file was stored in the attributes of a node, rather than in the child nodes. All of the examples I have seen from Macromedia show the XML formatted with the data contained in child nodes.

What is interesting and somewhat disconcerting is that your XML data can be formatted either way and you do not need to make any changes to your code.

So what happens if you have an attribute name with some data and a child node of the same name? In this case the attribute wins and takes precedence over the child node.

I took the "Drilling Down into Data" example and modified it to load one of three different XML files on button click. You can view it below and see how each XML file is used.

View Example
View Source MXML
View Attribute XML
View Node XML
View Node XML with an Attribute

I find it confusing that you can use either type of XML without any changes to your code. I think it would make more sense if you used a "@" when you wanted to access an attribute rather than a node. This is how you access attributes with E4X in actionscript 3.

So:

mx:ComboBox x="25" y="60" width="190" id="locationSelector" labelField="title" dataProvider="{contentData.tourContent.mLocation}"

could be

mx:ComboBox x="25" y="60" width="190" id="locationSelector" labelField="@title" dataProvider="{contentData.tourContent.mLocation}"

November 28, 2005

Drilling Down into Data

This demo shows the ability of Flex to delve deeply into nested data with small amounts of intuitive code.

Here is an example I created.
View Example (Flash Player 8.5 required)
View Source MXML
View Source XML

When you first launch the demo it loads the "content.xml" file. This is caused by the "initialize="contentDataService.send()" tag in the mx:Application node and of course the contentDataService mx:HTTPService.

Once the data is loaded here is what happens:

The select location comboBox is populated with the result from the service call. The first item (index 0) is automatically selected.

The location description box pulls the "selectedItem" from the locationSelector combobox and snatches the description attribute from it.

The tour stop pulls its data from the selectedItem in the locationSelector combobox.

Tour stop description, image dataGrid and pan dataGrid pull data from the selectedItem of the tourStopSelector comboBox.

Image and pan descriptions are pulled from the corresponding dataGrids.

The URL List is populated from the selected Pan in the pan dataGrid.

And finally the URL path window pulls the path from the selected URL in the URLdatagrid.

This way of referencing data is really slick! Each item is using the selected bit of a "parent item" as its datasource instead of having to create an absolute reference to the data using the orginal mx:HTTPService call. It like each component has its only little "scope" to work within. Plus, Flex will automatically take care of all the databinding for you. Whenever you change the selection of something all dependent items are updated automatically. This is easy to as you play around with the demo.

So let’s compare the amount of code it takes to reference data with the relative method vs. an absolute path. Looking at the final URL path portion of a URL assigned to a Pan.

Compare:

text="{panURLs.selectedItem.urlPath}"
vs.
text="{contentDataService.result.tourContent. mLocation[locationSelector.selectedIndex]. tourStop[tourSelector.selectedIndex].pan[tourPans.selectedIndex]. url[panURLs.selectedIndex].urlPath}"

It is interesting to note that the automatic databinding does not work correctly when using the absolute path in this way. Strangely it does work when you are viewing the URL paths within My Home -> Living Room -> Living Room Pan 1, but not in any others. No sure why...

One other thing I wanted to mention is the use of the labelField and ColumnName tags. By default many components will look for a "label" attribute or node to use as the label for each item they contain. When you set the labelField (or ColumnName) to a different value it will look for that field to display instead.