4 Replies Latest reply on Aug 19, 2007 12:41 PM by ntsiii

    The difference between getItemAt() and []

    Level 7
      Can somebody explain in simple terms what the difference between these
      code blocks is, and why the former works correctly, but the latter does not?

      <mx:Binding destination="filterParams.severity_min"
      source="filter_severity.values[0]"/>
      <mx:Binding destination="filterParams.severity_max"
      source="filter_severity.values[1]"/>

      This works as desired, but produces the following warnings in my IDE.

      "Data binding will not be able to detect changes when using square
      bracket operator. For Array, please use ArrayCollection.getItemAt()
      instead."

      But using the suggested form, produces incorrect results, no value is
      returned.

      <mx:Binding destination="filterParams.severity_min"
      source="filter_severity.values.getItemAt(0)"/>
      <mx:Binding destination="filterParams.severity_max"
      source="filter_severity.values.getItemAt(1)"/>
        • 1. Re: The difference between getItemAt() and []
          flexPro
          It sounds like both of the above snippets compile for you, based on your explanation. In that case, I'm guessing filter_severity.values is one of the ListCollectionView subclasses, since I don't know of any others that support both getItemAt() and []. If so, there is absolutely no difference between [] and getItemAt() in Flex 2.01 (I just checked the SDK code and [] calls getItemAt() directly).

          Some questions to try to get to the bottom of this:

          Are you using Flex 3 (haven't looked at the source there)?

          Is filter_severity.values something other than ArrayCollection or XMLListCollection?

          Do both snippets compile without changing anything else in the code?

          If you do change something else in the code, post it and I'll try to help.
          • 2. Re: The difference between getItemAt() and []
            Level 7
            flexPro wrote:
            > It sounds like both of the above snippets compile for you, based on your
            > explanation. In that case, I'm guessing filter_severity.values is one of the
            > ListCollectionView subclasses, since I don't know of any others that support
            > both getItemAt() and []. If so, there is absolutely no difference between []
            > and getItemAt() in Flex 2.01 (I just checked the SDK code and [] calls
            > getItemAt() directly).
            >
            > Some questions to try to get to the bottom of this:
            >
            > Are you using Flex 3 (haven't looked at the source there)?
            >
            > Is filter_severity.values something other than ArrayCollection or
            > XMLListCollection?
            >
            > Do both snippets compile without changing anything else in the code?
            >
            > If you do change something else in the code, post it and I'll try to help.

            No I am not using Flex 3.

            Umm, filter_severity is a multi-thumb slider so I presume values is an
            array, I don't think it is an ArrayCollection.

            Yes both snippets compile. That latter compiles without any errors or
            warnings, but the result is always a value of 0, former compiles with
            the given warnings, but works correctly, providing the selected values
            of both slider thumb controls.

            I'm not planning on changing anything at this time, I'll live with the
            compiler|IDE warnings but working code.

            I'll append the the entire MXML file here.

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Panel
            xmlns:mx=" http://www.adobe.com/2006/mxml"
            xmlns:Ctrl="Components.Controls.*"
            layout="vertical">

            <mx:Metadata>
            [Event('filterUpdateEvent')]
            </mx:Metadata>

            <mx:Script>
            <![CDATA[
            import mx.utils.ObjectUtil;
            import mx.controls.Alert;

            [Bindable]
            public var severityAry:Array;

            private function filter_severity_init():void
            {
            filter_severity.setThumbValueAt(0,filter_severity.minimum);
            filter_severity.setThumbValueAt(1,filter_severity.maximum);
            }

            private function addAllElement():void
            {
            filter_status.dataProvider.source.unshift("All");
            filter_status.selectedIndex = 0;
            }
            ]]>
            </mx:Script>

            <mx:Model id="filterParams">
            <filters>
            <status></status>
            <severity_min></severity_min>
            <severity_max></severity_max>
            </filters>
            </mx:Model>

            <mx:Binding destination="filterParams.status"
            source="filter_status.selectedItem"/>
            <mx:Binding destination="filterParams.severity_min"
            source="filter_severity.values[0]"/>
            <mx:Binding destination="filterParams.severity_max"
            source="filter_severity.values[1]"/>

            <mx:Form >
            <mx:FormItem label="Status:" width="100%">
            <Ctrl:statusComboBox
            creationComplete="addAllElement()"
            id="filter_status"
            width="100%"
            change="{dispatchEvent (new Event('filterUpdateEvent'))}"/>
            </mx:FormItem>

            <mx:FormItem label="Severity:" width="100%">
            <Ctrl:severitySlider width="100%"
            id="filter_severity"
            change="{dispatchEvent (new Event('filterUpdateEvent'))}"
            allowThumbOverlap="true"
            thumbCount="2"
            creationComplete="filter_severity_init()"/>
            </mx:FormItem>
            </mx:Form>
            </mx:Panel>
            • 3. Re: The difference between getItemAt() and []
              flexPro Level 1
              Thanks for the clarifications. You're correct that filter_severity.values is an Array, which means that the method getItemAt() doesn't exist (hence the 0 values you're getting). Not sure why this still compiles, but you can put anything in place of getItemAt (e.g. imaginaryMethod()) without warning or error. Don't have time to puzzle through this just now, especially since your code works as is. Quickly, though, if you'd like to get rid of the warnings, one way would be binding to the Array values itself and then reading the values out of the binding destination.
              • 4. Re: The difference between getItemAt() and []
                ntsiii Level 3
                "... Not sure why this still compiles..." I suspect that Array is a dynamic class, and with dynamic classes you can call non-existing methods and properties without the compiler complaining.

                Generally, do not use Array for any binding source. Wrap it in an ArrayCollection, which does support binding with getItemAt.

                Tracy