我正在为新工作开发一个 Flex 应用程序。这有点像一个训练轮应用程序——我正在学习这门语言,这不是一个需要与服务对话才能完成工作的应用程序。整个应用程序中有一些组合框实例共享相同的一组可能值(例如,选择状态:“进行中”、“拒绝”、“完成”),我希望它们使用相同的数据源.
管理此问题的最佳方法是什么?
最佳答案
MVC 架构 ....在简单的情况下只是模型部分:
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;
}
}
然后您可以从任何组件更新和使用单例,如下所示:
[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 集合所做的任何更改都将在两个组件中更新。
关于apache-flex - 在 Flex 中的组件之间共享数据的最佳方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1104701/