How to create instance model that can be shared throughout a component (which can be created more than once)

StackOverflow https://stackoverflow.com/questions/2874429

문제

I am trying to create a modular structure that will eventually live inside another actionscript/flex application. I am using cairngorm in the outside application and I'd like to create more than one instance of this new component in the inside application. Cairngorm doesn't work for creating multiple duplicate components because of the singleton nature of the model.

The component I'm creating is a pseudo video viewer. I'd like to have a shared model within this component instance. I'm struggling with a way to share a single model within the component. If I use the singleton pattern, I can't create more than one instance of this component. If I create an instance and let the component make sure there is only one, then I have trouble passing the model easily to all components that want it. This is especially hard because .mxml components can't have a constructor where I would pass in the model.

I was thinking of having each component ask its parent for the module until a component had it. This would require that each component have that interface implemented and feels a bit icky to me.

Does anyone know a way to create an instance variable within a component so that multiple components can be create, but also share that model easily throughout that component?

도움이 되었습니까?

해결책

The newest Swiz release (1.0.0) has the answer to this question. You can now create more than one instance of Swiz in a project and setup a parent child relationship. The module support page is where they explain it best (http://swizframework.jira.com/wiki/display/SWIZ/Module+Support) but you don't actually have to use modules, any parent child relationship will work.

Just remember that in order to keep every event handler from responding to an event from a child view, you need to set the EventHandler scope to local, for example:

[EventHandler( event="TestEvent.SAVE_USER_REQUESTED", scope="local")]
public function handleEvent(event:Event):void {
    //do stuff
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top