在 Flex 中的组件之间共享数据的最佳方式是什么? [英] What's the best way to share data between components in Flex?
问题描述
我有一个 Flex 应用程序,我正在为一份新工作工作.这是一个训练轮应用程序——我正在学习语言,这不是一个需要与服务对话才能完成工作的应用程序.整个应用程序中有几个组合框实例共享相同的一组可能值(例如,一组状态:进行中"、拒绝"、完成"),我希望使用相同的数据源.
I have a Flex application that I'm working on for a new job. It's sort of a training wheels application -- I'm learning the language, and this isn't an app that needs to talk to a service in order to do its job. There are a few instances of combo boxes throughout the application that share the same set of possible values (say, a selection of states: "In Progress", "Rejected", "Complete") that I want to have use the same data source.
管理此问题的最佳方法是什么?
What is the best way to manage this?
推荐答案
MVC 架构 ....在简单的情况下只是模型部分:
MVC architecture ....well in simple cases just the Model part:
package
{
[Bindable]
public final class ShellModelSingleton
{
public var selectedStatus:ArrayCollection;
////////////////////////////////////////////
// CONSTRUCTOR
// ****DO NOT MODIFY BELOW THIS LINE*******
///////////////////////////////////////////
public function ShellModelSingleton(){}
/****************************************************************
* Singleton logic - this makes sure only 1 instance is created
* Note: you are able to hack this since the constructor doesn't limit
* a single instance
* so make sure the getInstance function is used instead of new
* ShellModelSingleton()
*****************************************************************/
public static function getInstance():ShellModelSingleton {
if(_instance == null) {
_instance = new ShellModelSingleton();
}
return _instance;
}
protected static var _instance:ShellModelSingleton;
}
}
然后你可以像这样从任何组件更新和使用单例:
Then you can update and use the singleton from any component like this:
[Bindable] private var model:ShellModelSingleton =
ShellModelSingleton.getInstance();
组件 1
<mx:DataGrid id="myDG" dataProvider="{model.selectedStatus}" />
组件 2
<mx:List id="myList" dataProvider="{model.selectedStatus}"
labelField="label" />
然后,您对 selectedStatus 集合所做的任何更改都将在两个组件中更新.
Then any changes you make to the selectedStatus collection will be updated in both components.
这篇关于在 Flex 中的组件之间共享数据的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!