I am currently trying to implememnt a Singleton Class in order to store a ArrayCollection of items that I can then access and manipulate across the lifecycle of my app. I have created the below Singleton Class that is designed to hold the ArrayCollection information:
package valueObjects
{
import mx.collections.ArrayCollection;
[Bindable]
public class Model
{
private static var instance:Model = new Model();
public var ids:ArrayCollection = new ArrayCollection();
public function Model()
{
if(instance)
{
trace("New instance cannot be created. Use Singleton.getInstance()");
}
}
public static function getInstance():Model
{
return instance;
}
}
}
I have then created the following code on the Main Deafult page for my application so that the ArrayCollection is populated as soon as the app is initiated:
import valueObjects.Model;
protected var models:Model = new Model();
private function loop():void
{
var index:int;
for( index = 0; index < compsCollection.length; index++ )
{
trace( "Element " + index + " is " + compsCollection[index].comp_id );
models.ids.addItem(compsCollection[index].comp_id);
trace(models.ids.length);
}
}
The ArrayCollection in the Singleton Class is being populated as the trace statement that I have entered into the loop clearly shows the build of of data in the ArrayCollection. However then when I move to another view within the application I then try to access this ArrayCollection within the Singleton Class with the following code:
import valueObjects.Model;
protected var models:Model = Model.getInstance();
protected var test:ArrayCollection = new ArrayCollection();
protected function view1_viewActivateHandler(event:ViewNavigatorEvent):void
{
var index:int;
trace("Array Length =" + models.ids.length);
for( index = 0; index < models.ids.length; index++ )
{
trace( "Element " + index + " is " + models.ids[index].comp_id );
test.addItem(models.ids[index].comp_id);
}
testbox.text = test.toString();
}
Now the problem I am having is that when I try to access this ArrayCollection(ids) it appears to be empty for some reason. I have included a trace statement that also says that the length of the ArrayCollection is “0”. Can anyone please help??