Flex中组件之间共享数据的最佳方式是什么? [英] What's the best way to share data between components in Flex?

查看:162
本文介绍了Flex中组件之间共享数据的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Flex应用程序,正在为新工作而工作。这是一个训练轮的应用程序 - 我正在学习语言,这不是一个应用程序,需要与服务交谈,以完成其工作。在整个应用程序中有几个组合框的实例共享同一组可能的值(比如,进行中,被拒绝,完成)的选择,我想要使用相同的数据源。

什么是最好的方法来管理这个?

解决方案

MVC架构....以及简单的情况下,模型部分:

  package 
{


[Bindable]
public final class ShellModelSingleton
{


public var selectedStatus:ArrayCollection;




////////////////////////////// //////////////
// CONSTRUCTOR
// ****不要在这行下面修改*******
// ////////////////////////////////////////
公共函数ShellModelSingleton() {}

/ *************************************** *************************
*单例逻辑 - 这确保只有一个实例被创建
*注意:你是能够破解这个,因为构造函数不限制
*单个实例
*所以确保使用getInstance函数而不是新
* ShellModelSingleton()
*** ************************************************** ************ /
public static function getInstance():ShellModelSingleton {
if(_instance == null){
_instance = new ShellModelSingleton();
}
return _instance;
}

protected static var _instance:ShellModelSingleton;
}

}

然后你可以更新和使用从这样的任何组件的单身人士:

pre




$ b $

组件1

 < mx:DataGrid id =myDGdataProvider ={model.selectedStatus}/> 

组件2

 < mx:List id =myListdataProvider ={model.selectedStatus}
labelField =label/>

然后,您对selectedStatus集合所做的任何更改都将在这两个组件中进行更新。 b $ b

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 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();

Component 1

<mx:DataGrid id="myDG" dataProvider="{model.selectedStatus}" />

Component 2

   <mx:List id="myList" dataProvider="{model.selectedStatus}" 
      labelField="label" />

Then any changes you make to the selectedStatus collection will be updated in both components.

这篇关于Flex中组件之间共享数据的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆