4 Replies Latest reply on Mar 17, 2011 7:23 AM by Flex harUI

    Creating Components On-Fly From String

    mattcom Level 1

      So, I've been playing around a while with the concept of being able to pass a string from a database into flex, and then flex being able to generate stuff on the fly.  Here's my code so far:

      <?xml version="1.0" encoding="utf-8"?>

      <mx:Application

       

       

      xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="intialBreakup()">

       

       

       

      <mx:Script>

      <![CDATA[

       

       

      import mx.controls.Text;

       

      import mx.controls.Label;

       

      import mx.controls.TextInput;

       

       

      private function intialBreakup():void

      {

       

       

      var sample:String = 'This is an example[TextInput id="Bob" width="5"]that I am[TextInput id="joe"]trying to work with';

       

      var bracketCount:Array = sample.split("[");

       

      var compMe:RegExp = /\[.*?\]/ig;

       

      var labelMe:RegExp;

       

      for(var i:int = 1; i < bracketCount.length; i++)

      {

       

      //compMe = /\[(\w*)\]/ig;

       

       

      if(i == 1)

      {

      labelMe =

      /.*?\[/;

      }

      else {

      labelMe =

      /\].*\[/i;

      }

      makeLabel(labelMe.exec(sample));

      makeComponent(compMe.exec(sample));

      }

      }

       

       

      private function makeComponent(original:String):void

      {

       

       

      var unBracketed:String = str_replace(original, "[", "");

      unBracketed = str_replace(unBracketed,

      "]", "");

       

      //var compObject:Object = unBracketed.split(" ");

       

       

      var compArray:Array = unBracketed.split(" ");

       

      //var Temp:ClassFactory;

       

       

      switch(compArray[0])

      {

       

      case "TextInput":

       

      var Temp:TextInput = new TextInput();

       

      break;

      }

       

      // now add the parameters

       

       

      for(var i:int = 1; i<compArray.length; i++)

      {

       

      var paramArray:Array = getParam(compArray[i]);

       

      switch(paramArray[0])

      {

       

      case "id":

      Temp.id = paramArray[1];

       

      break;

       

      case "width":

      Temp.width = paramArray[1];

       

      break;

      }

      }

      question.addChild(Temp);

      }

       

       

      private function getParam(original:String):Array

      {

       

      var unquoted:String = str_replace(original, '"', '');

       

      var paramSplit:Array = unquoted.split("=");

       

      return paramSplit;

      }

       

       

      private function makeLabel(original:String):void

      {

       

       

      var textMe:Label = new Label();

      textMe.text = str_replace(original,

      "[", "");

      textMe.text = str_replace(textMe.text,

      "]", "");

      question.addChild(textMe);

      }

       

       

      private function str_replace(original:String, found:String, replaced:String):String

      {

       

      var array:Array = original.split(found);

       

      return array.join(replaced);

      }

       

      ]]>

       

      </mx:Script>

       

       

      <mx:Text text="This is an example[TextInput id='Bob' width='5']that I am[TextInput id='joe']trying to work with"/>

       

       

      <mx:HBox id="question"/>

      </mx:Application>

       

      My problem is that I'd have to create many case statements in order to further it along.  Is there some kind of type for Temp, such as Temp:ClassFactory or something that is generic enough to say something like    Temp = new compArray[0](), and can you think of a way to do the same with the params, such as Temp.paramArray[0] = paramArray[1] so that I can avoid using case statements?