Steven
Steven

Reputation: 1979

Reloading a component but maintaining the same instance in Flex

Let's say that I have a canvas component called myCanvas and I instantiated it with

var myCanvasPage:myCanvas = new myCanvas;
this.addChild(myCanvasPage);

Then I want to essentially reload myCanvasPage later on. In effect I want to do this:

this.removeChild(myCanvasPage);
var myCanvasPage:myCanvas = new myCanvas;
this.addChild(myCanvasPage);

except instead of creating a new instance of myCanvas, I want to use the already created instance of myCanvas and essentially reload or re-create it but making sure that the instance name and references to it remain the same. What would be the best way to do this?

Upvotes: 0

Views: 432

Answers (3)

user610650
user610650

Reputation:

Woa I'm not sure where to start here... I think THE_asMan addresses most of your misunderstandings. The missing bit I think you need to look into is how a reference to an object holds (i.e. is counted) as long as it doesn't go out of scope. Just keep in mind that as long as the a variable is not out of scope, its reference to some object (if there is one, i.e. if it is not null) is counted.

http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f9d.html#WS5b3ccc516d4fbf351e63e3d118a9b90204-7f8c

Upvotes: 0

The_asMan
The_asMan

Reputation: 6403

Whenever you do

var myCanvasPage:myCanvas = new myCanvas;

you are instantiating the myCanvasPage Object.
Removing an object from the stage will not delete the Object from memory.
As long as there is a reference somewhere in your code the myCanvasPage Object will not be garbage collected and all the values and attributes will be the same as at the time you removed it from the stage.
To make that reference you have to scope it to your class.

public var myCanvasPage:myCanvas = new myCanvas;

So now you would reference it with

this.myCanvasPage

and when you are ready to add it back to the stage.

this.addChild(this.myCanvasPage);

So to recap you can add and remove an Object from stage

this.addChild(this.myCanvasPage);
this.removeChild(this.myCanvasPage);

all day long and the data in that object will never go away.

Some good reading on garbage collection

Upvotes: 1

Constantiner
Constantiner

Reputation: 14221

What do you mean as "reloading"? Maybe it is simpler to create data-driven components and change that data to change or reset component state>

Upvotes: 0

Related Questions