Flex Chronicles #12: External Text Files for String Variables

Typically, if you want external content in Flash, you link to an external text file, and load it into a text field like so:

lv = new LoadVars();
lv.owner = this;
lv.onData = function(str)
{
  this.owner.my_txt.text = str;
};
lv.load("yourTextFile.txt");

Quick and dirty, right? Well, Flex does the same thing for you, but without all the work; check this out:

<mx:String id="data_str" source="some.txt" />
<mx:TextArea text="{data_str}" />

Yes, that’s it, I swear! The line between authortime & compile time is blurred so technically this is compiled in, but it remains an external text file you can check into source control & deploy on your webserver, or keep locally.

Comments

3 responses to “Flex Chronicles #12: External Text Files for String Variables”

  1. pim Avatar

    We use to do that in our flash projects were we put the LoadVars in the _global scope.
    The nice thing is that you can ‘bind’ the a value to a TextField’s var attribute.

  2. Abdul Qabiz Avatar

    If you are looking for precompiled-swf deployement and external textfile changes very often, this solution might not be desirable. Because it requires compilation whenever external textfile changes.

    But this is a good way to manage source and keep mxml clean. I don’t know much about Flex compiler, but doing this a lot might increase compile time; compiler has to locate, load and compile the content.

    -abdul

  3. Massy Avatar
    Massy

    Using this way, I’ve some problems loading Unicode chars from external file. For example, italian characters like “ààè” are not displayed properly. Giving the value directly to the String is ok, but loading from external file is not good.
    If anyone have a solution to it, it would be very appreciate.