Flex user defined runtime settings Part 1

Tags: Flex,AS3
Word Count: 611

If you are looking to start developing Flex applications and you are coming from the web world you will hear the following statement many times. It takes time to get your head around the event model versus a traditional web request. This is so true and it can't be stressed enough, stop thinking to yourself how would I do this in (insert you preference here). So that kind of ties in to what I wanted to talk about. After you get a few smaller applications under your belt you start to realize that you need to a way get preferences / settings that a user can set. These are called runtime variables because you don't have to recompile the Flex application when these values change. There are two ways (that I know of) about going about this. You can read in a file such as an XML file or you could use flash vars.

This article is going to focus on reading in a XML file but I  hope to follow it up with a how to on flash vars because they can be very useful as well. For this tutorial we are going to create a config directory under our src folder. In the config directory we will create a simple XML file that contains some application settings.

The first thing most new developers will do is to read in the XML file using the Flex XML tag. If you were to run the example it would work just fine with 1 small problem. This tag is actually a compile time tag which means that when you compile the application the XML is included. You will not be able to change the file after it has been compiled. This obviously will not work as we need the ability for users to change settings without having to recompile the application. So that leaves us with actually reading in the XML file.

This is actually the solution we are looking for but its a little more than just reading the file. Coming from the web world and writing creation Complete events for all of my small applications I thought this would be easy as Sunday morning. The following is pretty basic code that we have all written at one time or another. The problem with this is we can read in our file but if we go to get the actual values right after that line they may not be ready yet. This all goes back to the idea that you need stop thinking of top down programming. We actually need to read in the settings file and than dispatch an event that the configuration file has been loaded. We can then then listen for that event and get our settings when its ready.

In part 2 of this article we will go  through the actual code that consists of a custom class and a custom event class. Any thoughts or comments please let me know.

Comments

#1 Posted By: radekg Posted On: 8/2/08 4:29 PM
Anuj Gakhar published something similar some time ago, you can read on it and preview sample app here: http://www.anujgakhar.com/2007/12/11/changing-mxml....
#2 Posted By: Dan Vega Posted On: 8/4/08 4:10 PM |
Author Comment
Thanks for the link. I still wanna finish up the article just so I can point out the problems I faced.
#3 Posted By: radekg Posted On: 8/4/08 4:36 PM
of course. It will be nice to see other implementation.


Post Your Comment







Show Captcha

If you subscribe, any new posts to this thread will be sent to your email address.

Copyright © 2007 Dan Vega | BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.