views:

246

answers:

1

Say I have code in my main mxml and in a function like this:

this.addChild(someContainer);

and now I want to refactor code and move this to it's own class and method in a separate file. How could I access root now, since this obviously now points to the new class I created.

+2  A: 

You could use something like Application.application, Application.root or event this.root (depending on your needs)

But I would'nt advise it (try to refactor your code so that you can pass a reference to your main or something like that ... ).

The question is : is your newly-created class going to be reponsible for adding childs to components ? Then I would suggest you instead make it possible to pass it a reference to any kind of Container ; and in you main mxml, you pass the reference to root.

It might also be that the new class is only responible for exposing the things to add ; in which case you could probably leave the "this.addChild(...)" code in the main.

Hoping this helps.

phtrivier
I tried passing this from Main into the class constructor to cache it and use it from there, but I couldn't access addChild from there. Application.application.addChild seems to do the trick.
Keyframe
You're right, I think i mixed Application.application, Application.root and this.root ... I'll fix my answer.
phtrivier