开发者

Can I make my MXML Component a Singleton?

I have an MXML component in a website that I am reusing in a few different screens. Based on what the user clicks, the component initializes a few values and starts playing media.

Now if I click through the site, and I play media in screen 1, the component initializes fine and plays the media.

If I then go to screen 2 and play t开发者_JAVA百科he media, the component initializes twice. Once for screen one, and once for screen 2.

When I then go to screen three, it will start initializing three times.. So it is creating a new instance of the MXML component for each screen!!

How can I make sure that the MXML component only initializes for the screen that I need it to initialize for?

What I really want is that this component always has just one instance throughout the whole application. Is it possible to make that MXML component into a Singleton, so that I always have one instance of that MXML in my application?


Can you explain this a little more indepth? What do you mean by "screens"? It sounds like you have your component nested in some sort of view stack, and that your screens are different sections in the view stack, but it's hard to tell here.

Regardless, I think the solution is to abstract the part of your component that you want to be a singleton from the view. Flex initializes objects in view stacks in a lazy manner by default, but this can be overridden using the creationPolicy property on the ViewStack object. It sounds like your creationPolicy is initializing children as you access them, and something in your component code is causing other instances of the same object to re-fire some initialization code when others are created (possibly in your experimenting for an MXML singleton.)

To truly achieve your desired effect, you should probably just write a bit of actionscript that intelligently re-parents the display object you only want to be created twice. The idea of a "singleton" doesn't make as much sense when we're talking about view objects on screens - to have it displayed in many places, you need many instances, and the process of re-parenting is slightly more complicated than the singleton pattern, so you'll need to do a bit of creative logic around that.

Hope this helps - again, please feel free to post some more source code if you want a more specific response.


Why don't you make the component into a module and use it that way. You Load and/or Unload a module and use it where ever you like! in just calling it as a single item! and you have very much less overheads in your application.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜