【问题标题】:dataProvider How to Point for a specific Object(Not by Index)dataProvider 如何指向特定对象(不是按索引)
【发布时间】:2015-03-27 17:09:49
【问题描述】:

如何使用已在该对象数组中的指定 ID 指向 DataProvider 中的对象。 例如,DataProvider 包含一个对象 - 它具有一些属性:模型的路径/地址('testHouse/testHouseMD2.md2'),以及它的 ID(“20”)。

我可以使用getItemAt(index("20").address 方法访问吗?或者它只能用于按索引获取对象,例如该对象具有 ID("20") 但它的索引可能指向“5”,因此当我将“20”放入 getItemAt 时,我得到完全错误的对象。

或者它需要创建一个自己的方法来访问对象属性(ID)以用作地址的pointer(Index)? 如果是这样,请帮助我知道可以使用哪些构造函数 - 请!!

【问题讨论】:

    标签: actionscript-3 indexing dataprovider


    【解决方案1】:

    反正我修好了这个 articles_ary[prevIndex].load(markers.@value);

    我不需要使用 DataProvider 等,该死的我没有。 还是谢谢你的帮助

    【讨论】:

    • 你可能想看看我最后的改动。
    【解决方案2】:

    你应该封装对对象的访问:

    const _map:Dictionary = new Dictionary();
    const _collection:IList = new ArrayCollection();
    
    function add(object:Object = null):Object {
      if (!object) return null;
      _map[object.id] = object;
      _collection.addItem(object);
      return object;
    }
    
    function getById(id:String):Object {
      if (!id) return null;
      return  object[id];
    }
    
    function getAt(index:uint):Object {
      if (index < 0) return null;
      return _collection.getItemAt(index);
    }
    

    如果您正在使用类 - 建议这样做,您应该为这些集合创建类型化接口以减少可能的访问错误的数量。

    您可能使用的是这样的东西,而不是 ArrayCollection:

    package io.floriansalihovic.collections {
    
      import flash.events.Event;
      import flash.events.IEventDispatcher;
      import flash.utils.Dictionary;
    
      import mx.collections.ArrayCollection;
      import mx.collections.IList;
    
      [Event(name="collectionChange", type="mx.events.CollectionEvent")]
    
      public class Collection implements IEventDispatcher, IList {
    
        /** Internal collection. */
        private var _collection:ArrayCollection;
    
        /** Dictionary for fast id lookup. */
        private var _dictionary:Dictionary;
    
        /** @inheritDoc */
        public function get length():int {
          return this._collection.length;
        }
    
        /**
         * The constructor.
         *
         * @param collection The default collection.
         */
        public function Collection(collection:ArrayCollection = null) {
          this._collection = collection || new ArrayCollection();
          this._dictionary = new Dictionary();
        }
    
        /** @inheritDoc */
        public function addEventListener(type:String,
                                         listener:Function,
                                         useCapture:Boolean = false,
                                         priority:int = 0,
                                         useWeakReference:Boolean = false):void {
          this._collection.addEventListener(type, listener, useCapture, priority, useWeakReference);
        }
    
        /** @inheritDoc */
        public function dispatchEvent(event:Event):Boolean {
          return this._collection.dispatchEvent(event);
        }
    
        /** @inheritDoc */
        public function removeEventListener(type:String,
                                            listener:Function,
                                            useCapture:Boolean = false):void {
          this._collection.removeEventListener(type, listener, useCapture);
        }
    
        /** @inheritDoc */
        public function hasEventListener(type:String):Boolean {
          return this._collection.hasEventListener(type);
        }
    
        /** @inheritDoc */
        public function willTrigger(type:String):Boolean {
          return this._collection.willTrigger(type);
        }
    
        /** @inheritDoc */
        public function addItem(item:Object):void {
          this._dictionary[item.id] = item;
          this._collection.addItem(item);
        }
    
        /** @inheritDoc */
        public function addItemAt(item:Object,
                                  index:int):void {
          this._dictionary[item.id] = item;
          this._collection.addItemAt(item, index)
        }
    
        /** @inheritDoc */
        public function getItemAt(index:int,
                                  prefetch:int = 0):Object {
          return this._collection.getItemAt(index, prefetch);
        }
    
        /**
         * Returns the object associated with the given id.
         * @param id The id of the object.
         * @return The object associated with the given id, null else.
         */
        public function getItemById(id:String):Object {
          return _dictionary[id];
        }
    
        /** @inheritDoc */
        public function getItemIndex(item:Object):int {
          return this._collection.getItemIndex(item);
        }
    
        /** @inheritDoc */
        public function itemUpdated(item:Object,
                                    property:Object = null,
                                    oldValue:Object = null,
                                    newValue:Object = null):void {
          this._collection.itemUpdated(item, property, oldValue, newValue);
        }
    
        /** @inheritDoc */
        public function removeAll():void {
          this._collection.removeAll();
          this._dictionary = new Dictionary();
        }
    
        /** @inheritDoc */
        public function removeItemAt(index:int):Object {
          const object:Object = this._collection.removeItemAt(index);
          delete _dictionary[object.id];
          return object;
        }
    
        /** @inheritDoc */
        public function setItemAt(item:Object,
                                  index:int):Object {
          this._dictionary[item.id] = item;
          this._collection.setItemAt(item, index);
        }
    
        /** @inheritDoc */
        public function toArray():Array {
          this._collection.toArray();
        }
      }
    }
    

    这将通过 id 提供访问权限。

    【讨论】:

    • 谢谢回复,我正在使用markers.xml &lt;marker id="1" adress="address="myMD2Models/testHouseMD2.md2"&lt;/marker&gt; 它用于在场景中加载模型,使用那种方法particles_ary[prevIndex].load(myDataGrid.dataProvider.getItemAt(0).address,myCarSkin, 10, 3);
    • 嘿弗洛里安,我使用 getById 时出错,它告诉我没有属性-object(它未声明),我不知道如何在 DataProvider 中使用它,比如更改 dataProvider.getById (“20”)如果是这样,我如何指向我需要的对象,因为这些数组(dataprovider)中有多个对象,如地址、名称、id 和其他?还是拿不到=((
    • 您使用的是 Flex 还是只使用 Flash?
    • 我正在使用这个pastebin.com/nfuC1Hzy,我想我找到了解决这个问题的可能方法,但我不知道该怎么做,重点是 - 从 .xml 标记中解析/排序指定的项目,item =“id”,并将其值分配给它被检索的对象的索引,就像当排序器到达 object(A).item(id) 时,它分配给 Object(A).Index(item(id) )),但我不知道如何在代码中做到这一点?你能帮帮我吗
    猜你喜欢
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 2022-06-13
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多