3 Replies Latest reply on Jul 22, 2009 8:49 AM by Paul Reilly

    Is XML object [Bindable] by default ?

    iamfuric Level 2

       

       

       

       

       

       

       

       

       

       

       

      Hello all

      Consider the popup window that lets you change few values that are stored in XML or discared changes and leave model with original values.

      Two input values are bound to model properties.

      How would you overcome the issue marked red in following code ?

      Should  i set initial values in action script code instead of using binding in XMXL?

      Another option is to define model as Object like this :

       

      private var model:Object=new XML (<nodes>

      <node label=

      "label1" data="data1"/>

       

       

      <node label=

      "label2" data="data2"/>

       

       

       

      </nodes>);

       

      so it is not [Bindable] and t1.text is not get updated when model is touched.

       

      What is your "pattern" for implementing saveChanges/discardChages  requirement?

      Thanks

       

      THE CODE :

       

       

      <mx:Script>

       

       

      <![CDATA[

       

       

       

       

       

      private var model:XML=<nodes>

      <node label=

      "label1" data="data1"/>

       

      <node label=

      "label2" data="data2"/>

       

       

      </nodes>

       

      private function update():void{

       

          model.node[0].@label=t0.text;

        // after the execution of previous line t1.text is changed back to original value

        // and we loose t1.text entered value

          model.node[1].@label=t1.text;

           closeMe();

       

      }

      private function closeMe():void{

       

         // close code

       

       

      }

       

       

       

       

       

      ]]>

       

       

       

       

       

      </mx:Script>

       

      <mx:TextInput id="t0" text="{model.node[0].@label}"/>

      <mx:TextInput id="t1text="{model.node[1].@label}"/>

      <mx:Button click="update()" label="Save changes" />

      <mx:Button click="closeMe()" label="Discard changes" />

        • 1. Re: Is XML object [Bindable] by default ?
          Paul Reilly Level 1

          I suggest having two local variables, something like:

           

            private var data0:String;

            private var data1:String;

           

          If the XML is defined at compile time, then you can initialize them directly:

           

            private var data0:String = model.node[0].@label;

            private var data1:String = model.node[1].@label;

           

          Otherwise, use a Binding two synchronize them:

           

            <mx:Binding source="model.node[0].@label" destination="data0"/>

            <mx:Binding source="model.node[1].@label" destination="data1"/>

           

          Then tie together the local variables and the TextInput controls.  If the XML is defined at compile time, you could do this in an creationComplete event handler.  If not, then you'll want to make data0 and data1 Bindable and let data binding initialize them:

           

            <mx:TextInput id="t0" text="{data0}"/>

            <mx:TextInput id="t1text="{data1}"/>

           

          Lastly, in your update() function, you can push the changes from data0 and data1 back into the XML object.  I should note that if the XML isn't defined at compile time and you don't use data binding, then you'll likely run into initialization issues.  For example, if data0 and data1 are created before model.  Data binding can be an expensive convenience, though, so if you know exactly when model is created, you could initialize data0 and data1.  You could even optimize them out, and assign directly from the model to t0.text and t1.text.  Then in your update() function, you could push them back to the XML object.  You probably don't want the model to have to know about the TextInput controls, so you might want to dispatch an event and have the document listen for that event.  Lots of options.  Good luck.

          • 2. Re: Is XML object [Bindable] by default ?
            iamfuric Level 2

            Thanks for the answer,Paul

            I think i would define XML model as Object to disable autobinding and update the model explicitely,

            this solution seems the simplest to me.

            Thanks again

            • 3. Re: Is XML object [Bindable] by default ?
              Paul Reilly Level 1

              I don't recommend doing that, because it will be timing sensitive and fragile.  If the creation of your XML gets deferred, like if you change the code to load it over the network at runtime, the initial binding with fail and since there won't be any watchers, updates won't be detected.