如何创建可以在整个组件中共享的实例模型(可以多次创建)
我正在尝试创建一个模块化结构,该结构最终将存在于另一个 actionscript/flex 应用程序中。我在外部应用程序中使用 cairngorm,并且我想在内部应用程序中创建这一新组件的多个实例。由于模型的单例性质,Cairngorm 不适用于创建多个重复组件。
我正在创建的组件是一个伪视频查看器。我想在此组件实例中拥有一个共享模型。我正在努力寻找一种在组件内共享单个模型的方法。如果我使用单例模式,我就无法创建该组件的多个实例。如果我创建一个实例并让组件确保只有一个实例,那么我很难将模型轻松传递给所有需要它的组件。这尤其困难,因为 .mxml 组件不能有一个构造函数来传递模型。
我正在考虑让每个组件向其父组件询问该模块,直到某个组件拥有该模块为止。这需要每个组件都实现该接口,这对我来说有点恶心。
有谁知道如何在组件中创建实例变量,以便可以创建多个组件,而且还可以在整个组件中轻松共享该模型?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最新的 Swiz 版本 (1.0.0) 给出了这个问题的答案。现在,您可以在一个项目中创建多个 Swiz 实例并设置父子关系。模块支持页面是他们对其进行最好解释的地方(http://swizframework.jira。 com/wiki/display/SWIZ/Module+Support),但实际上您不必使用模块,任何父子关系都可以工作。
请记住,为了防止每个事件处理程序响应子视图中的事件,您需要将 EventHandler 范围设置为本地,例如:
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: