3 Replies Latest reply on Apr 28, 2007 5:21 PM by netsesame

    Howto dynamicly bind XML element to a TextInput using BindingUtils?

    netsesame Level 1
      The question is: Howto dynamicly bind XML element to a TextInput component using BindingUtils?

      Now I use following codes:
      <!--
      [Bindable] private var xml: XML = <item><label>list1</label></item>;

      // initialize code for application's initialize event
      private function init(): void {
      BindingUtils.bindProperty(txtDemo, "text", xml, "label");
      }

      //click event
      private function test(): void {
      xml.label = "something";
      // txtDemo.executeBindings(); //---- no use anymore
      }
      -->
      <mx:TextInput id="txtDemo"/>
      <mx:Button label="Test" click="test()"/>

      //--------------------------------------
      My really idea is when bindable xml property is changed, then the textinput will be updated as hoped. However, no update happens to me when I click the Test button.

      But, if I make codes like that:
      <mx: TextInput id="txtDemo" text="{xml.label}"/>
      the text will updated when xml changs.

      So, what happened, I indeed need the dynamicly bind to the textinput compont.
      Help me. thanks.
        • 2. Re: Howto dynamicly bind XML element to a TextInput using BindingUtils?
          VarioPegged Level 2
          You could use an ObjectProxy since all subproperties will then be bindable:

          private var _xml:XML = <item><label>list1</label></item>;
          private var _opXML:ObjectProxy = new ObjectProxy(_xml);

          then in your init() function you declare _opXML as the host object with the bindable property "label"...

          // initialize code for application's initialize event
          public function init(): void {
          BindingUtils.bindProperty(txtDemo, "text", _opXML, "label");
          }

          //click event
          private function test(): void {
          _opXML.label = "something";
          }

          You'll notice that if you check your original _xml.label property with ChangeWatcher.canWatch(), it returns false. But if you create a dedicated object with a property that can be declared as bindable, canWatch() returns true. You'd probably be best off to write a lightweight class that can act as your model if you want to work with dynamic XML binding using Actionscript. This will allow you to use a bindable getter and setter that will give you the dynamic functionality you're looking for.

          Hope that helps.
          • 3. Re: Howto dynamicly bind XML element to a TextInput using BindingUtils?
            netsesame Level 1
            Thanks to VarioPegged.

            Yeah, objectProxy, it does work. However, it seems to support only object style XML, such as <item><label>list1</lable></item>. However, xml as <item label="list1"/>, it does not support.

            Your suggestion about write a lightweight class to wrap the XML features, it is a good idea. But, there are not fixed number's field in my xml data, that is, the fields or attributes are dynamic changed as user input. So, a lightweight class is not easy to design.

            The code of ObjectProxy.as gives me some tips. Maybe I can design a class to work for XML like that. However, it seems a big work.

            Anymore, thanks a lot.