5 Replies Latest reply on Sep 6, 2010 3:15 AM by JabbyPandaUA

    databinding and square brackets

    toft Level 1

      'Data binding will not be able to detect changes when using square bracket operator.'

       

      Is this an error in the compiler ?

       

      I have a setup that uses square brackets in a binding expression and it works perfectly well, despite the message telling me otherwise.

      (Yes I have checked to see if there is another source for the successful updates)

       

      Any idea ?

        • 1. Re: databinding and square brackets
          BhaskerChari Level 4

          Hi,

           

          In Flex Data binding will not be able to detect changes when using square bracket operator. For Array, we need to use ArraCollection.getItemAt(index) instead.

           

          Check the below example....

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
          <mx:Script>
            <![CDATA[
             import mx.collections.ArrayCollection;
             [Bindable]
             public var array:Array = new Array("HELLO","HOW","ARE YOU");
              
            ]]>
          </mx:Script>

          <mx:Text id="txt1" text="{array[0]}" left="100" top="0"/>
          <mx:Text id="txt2" text="{new ArrayCollection(array).getItemAt(0)}" left="100" top="50"/>

          </mx:Application>

           

          For the first text you will get the warning and for the second text you won't because you are using the ArrayCollection for binding which dont include any Square bracket operator.

           

          You will get only a compiler warning message when you use [] operator with Array, it's not an compiler error.

           

          Hope this will make things clear.

           

          If this post answers your question or helps, please kindly mark it as such.

           

           

          Thanks,

          Bhasker Chari

          .

          • 2. Re: databinding and square brackets
            toft Level 1

            Thanks for your thorough reply Bhasker.

             

            From my code:

             

            <s:TextArea id="textarea"

            fontFamily="{font_style_list.selectedItem['fontName']}"

            </s:TextArea>

             

            ..where font_style_list is a combobox.

             

            My point was that this actually work, despite the warnings claiming the oposite.

             

            Of course, something that works is not a problem, I am just curious why it works.

            • 3. Re: databinding and square brackets
              Shongrunden Adobe Employee

              This simple example demonstrates that it doesn't work:

              <s:Application
                  xmlns:fx="http://ns.adobe.com/mxml/2009"
                  xmlns:s="library://ns.adobe.com/flex/spark">
                  <fx:Script>
                      <![CDATA[
                          [Bindable] public var num:Number = 0;
                      ]]>
                  </fx:Script>

               

                  <s:VGroup>
                      <s:Button label="increment num" click="num++;" />
                      <s:Label text="{num}" />
                      <s:Label text="{this.num}" />
                      <s:Label text="{this['num']}" />
                  </s:VGroup>

               

              </s:Application>

               

              I suspect that your example appears to work because selectedItem is bindable and you are changing that value rather than changing the value of fontName in a constant selectedItem.

              • 4. Re: databinding and square brackets
                toft Level 1

                Thanks for your reply Shongrunden.

                 

                Being an employee I presume you are correct. However, I cant find (yet) the code that changes the value of itemSelected. Going to take a closer look later to figure this out.

                • 5. Re: databinding and square brackets
                  JabbyPandaUA Level 3

                  Documented approach to make binding work with square operator via ObjectProxy and getter function:

                   

                  http://ticore.blogspot.com/2008/03/flex-tip-bracket-operator-in-data.html