1 Reply Latest reply on Dec 28, 2016 9:44 PM by weik27118210

    AutoComplete get selectedItem() return null

    weik27118210

      I use autoComplete component:

      public function set selectedItem(value:Object):void {

        if (_selectedItem != value) {

            _selectedItem = value;

            _selectedItemChanged = true;

       

            invalidateProperties();

        }

      }

       

      public function set selectedItems(value:ArrayCollection):void {

            _initialSelectedItems = value;

           _selectedItemsChanged = true;

        

           invalidateProperties();

      }

       

      [Bindable(event="change")]

         public function get selectedItem():* {

              if (_selectedItems && _selectedItems.length > 0) {

                  return _selectedItems.getItemAt(_selectedItems.length - 1);

               } else {

                  return null;

               }

          }

       

      in commitproperties function, selectedItems add item:

      override protected function commitProperties():void {

          super.commitProperties();

          var item:Object;

          if (_selectedItemChanged && _dataProvider) {

               _selectedItemChanged = false;

                if (_selectedItem) {

                    _selectedItems.addItem(_selectedItem);

                }

          }

       

          if (_selectedItemsChanged && _dataProvider) {

               _selectedItemsChanged = false;

              _selectedItems.removeAll();

              for each (var selectedItem:Object in _initialSelectedItems) {

                  var foundItem:Object = null;

                  for each (item in _dataProvider.list) {

                      if (item == selectedItem) {

                         foundItem = item;

                     } else if (item.hasOwnProperty(_keyField) && item[_keyField] == selectedItem[_keyField]) {

                         foundItem = item;

                     }

                     if (foundItem) {

                          _selectedItems.addItem(foundItem);

                          foundItem = true;

                          break;

                     }

             }

             if (foundItem == null) {

                  _selectedItems.addItem(selectedItem);

              }

          }

        }

      }

       

      One Command(com.adobe.cairngorm.commands.Command) to setselectedItem:

          var ac = new AutoComplete();

          var targetItem = ...

           if (targetItem) {

                 ac.selectedItems = new ArrayCollection([targetItem]);

                 ac.selectedItem = targetItem;

            }

       

      and then get ac's value:

      if (this.ac.selectedItem) {

        return this.ac.selectedItem["value"];

      } else {

         return "";

      }

      here, this.ac.selecteditem is null, in commitproperties function, the _selectedItems add element, and in next frame, the commitproperties function be invoked

       

      if i get ac's value later 5 seconds after set selectedItems, the ac's vlaue is not null

       

      is it because i create many compontents in for loop, and the commitproperties cannot be invoked immediately, or how to make the commitproperties immediately, for example, use evt.dispatch()?

       

      how to make sure when i use ac.selecteditem, can return not null value?