Вы должны encapulate доступа к объектам:
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();
}
}
}
Это обеспечит доступ ид.
thx для ответа, im работает с markers.xml ' 'его используется для загрузки моделей в сцене, используя это вроде метода 'particle_ary [prevIndex] .load (myDataGrid.dataProvider.getItemAt (0) .address, myCarSkin, 10, 3);' –
LightFusion
Эй, Флориан, я получаю ошибку, используя getById, это говорит мне, что нет объекта propertytie (его не объявлено), и я не понимаю, как использовать его в DataProvider, например change dataProvider.getById («20»), если да, то как я указываю на объект, который мне нужен в этом массиве (dataprovider), являются несколькими объектами ( – LightFusion
Вы используете Flex или просто Flash? –