7 Replies Latest reply on Feb 23, 2007 7:21 AM by Cheree Singal

    Using 1 array collection as dataProvider for multiple combos

    Cheree Singal
      Hi,

      I have many combo boxes across my application that need the same data. I want to declare & populate a singe ArrayCollection at 1 place that should be used as dataProvider by all combos. Here, I want to mention that I have divided my app in different mxml components that are then declared in main mxml having Application tag. Any suggestions?

      Thanks in advance,
      Cheree
        • 1. Re: Using 1 array collection as dataProvider for multiple combos
          peterent Level 2
          Create a component that extends ComboBox and give it your data. Then use your ComboBox in place of an <mx:ComboBox>. If you make your data a static member of this new component it will be defined once.
          • 2. Re: Using 1 array collection as dataProvider for multiple combos
            Cheree Singal Level 1
            Hi,
            Thanks for such a prompt reply!
            My problem is that I am populating the dataProvider using RemoteObject. I tried your approach also after posting the query but the Remote Object gets called as many times as the number of combos even when I have made every thing static. Here is the code:


            <?xml version="1.0" encoding="utf-8"?>
            <mx:ComboBox xmlns:mx=" http://www.adobe.com/2006/mxml" labelField="sourceName"
            height="20" xmlns:common="Common.*" dataProvider="{dataSourceData}"
            creationComplete="initApp()">
            <mx:Script>
            <![CDATA[
            import mx.rpc.events.ResultEvent;
            import mx.collections.ArrayCollection;
            import mx.rpc.remoting.RemoteObject;

            public static var dataSourceList: ArrayCollection;
            public static var testDelegate: RemoteObject = new RemoteObject("AdminDelegate");

            public static var dataSourceData: ArrayCollection = new ArrayCollection();

            public static function initApp(): void{
            //testDelegate.concurrency = "single";
            //testDelegate.showBusyCursor = true;
            testDelegate.getDataSources.addEventListener("result", populateDataSources);
            testDelegate.getDataSources();
            }
            public static function populateDataSources(event: ResultEvent): void{
            dataSourceList = testDelegate.getDataSources.lastResult;
            for(var i: int = 0; i < dataSourceList.length; i++){
            dataSourceData.addItem(dataSourceList.getItemAt(i));
            }
            }

            ]]>
            </mx:Script>
            <mx:itemRenderer>
            <mx:Component>
            <common:ComboDropDownRenderer/>
            </mx:Component>
            </mx:itemRenderer>
            </mx:ComboBox>


            Can u tell me where I am going wrong?

            Thanks,
            Cheree
            • 3. Re: Using 1 array collection as dataProvider for multiple combos
              peterent Level 2
              Ah, you don't want the RemoteObject call inside the ComboBox component. You want to make the call once, in the main application, then provide the ComboBox with the data.

              For example, suppose you declare
              [Bindable] comboData:ArrayCollection;
              in your main application. You then invoke your RemoteObject call and the result handler populates comboData.

              Your ComboDropDownRenderer should override commitProperties and set its dataProvider:

              override protected function commitProperties() : void {
              super.commitProperties();
              dataProvider = Application.application.comboData;
              }

              If the event timing doesn't work out, set up a binding inside of your ComboDropDownRenderer:

              dataProvider="{comboData}" // in the <mx:ComboBox> root tag

              [Bindable] private var comboData:ArrayCollection;

              Then in commitProperties, set comboData = Application.application.comboData;
              • 4. Re: Using 1 array collection as dataProvider for multiple combos
                Cheree Singal Level 1
                I am sorry to say but it seems this is beyond my understanding:-(
                Can u exlain it in a simpler language...
                • 5. Re: Using 1 array collection as dataProvider for multiple combos
                  Cheree Singal Level 1
                  I want to emphasize that ComboDropDownRenderer is just to give a tooltip to the dropdown list.
                  • 6. Re: Using 1 array collection as dataProvider for multiple combos
                    Cheree Singal Level 1
                    Let me tell you 1 more approach that I followed.
                    I declared a Remote Object & an array collection in the main Application mxml. Invoked the rem object whose result event populated the arrayCol and then gave dataProvider to the combo as {parentApplication.arrayCol}. It gets populated but on closing the combo, I get this error:


                    RangeError: Index '4' specified is out of bounds.
                    at mx.collections::ListCollectionView/getItemAt()
                    at mx.collections::ListCollectionView/ http://www.adobe.com/2006/flex/mx/internal::getBookmark()
                    at ::ListCollectionViewCursor/get bookmark()
                    at mx.controls::ComboBase/set selectedIndex()
                    at mx.controls::ComboBox/set selectedIndex()
                    at mx.controls::ComboBox/close()
                    at mx.controls::ComboBox/mx.controls:ComboBox::focusOutHandler()
                    at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunction()
                    at flash.events::EventDispatcher/dispatchEvent()
                    at mx.controls::Button/ http://www.adobe.com/2006/flex/mx/internal::buttonPressed()
                    at mx.controls::Button/mx.controls:Button::mouseDownHandler()
                    at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunction()
                    at flash.events::EventDispatcher/dispatchEvent()
                    at mx.controls::ComboBase/::textInput_mouseEventHandler()


                    Here '4' is the number of objects in the arrayCol.

                    Any insights?

                    Thanks,
                    Cheree
                    • 7. Re: Using 1 array collection as dataProvider for multiple combos
                      Cheree Singal Level 1
                      Hmmm, commitProperties() making sense slowly... but I still dont understand why it has to be given inside ComboDropDownRenderer.

                      Cheree