是否可以从单独的ActionScript类更改flex图表的数据提供程序?

时间:2013-11-28 09:59:21

标签: actionscript-3 flex flex4 flex3 flex4.5

我试图从单独的动作脚本类更改flex图表数据提供程序?可能吗。我没有找到任何方法来做到这一点。任何想法的家伙?

2 个答案:

答案 0 :(得分:0)

是的,你可以。为此,您需要引用图表的数据提供者,它应该是可绑定的。这意味着当您更新dataProvider时,您的视图也将更新(在本例中为图表)。 如果它对您没有帮助,我会看到您的代码。

答案 1 :(得分:0)

是的,你可以。一个很好的干净方法是拥有自定义事件调度程序外观:

package com.app.facades
{
    import flash.events.Event;
    import flash.events.EventDispatcher;

    [Event(name="showDataInGrid"   , type="com.app.events.GridEvent")]
    public class GridFacade extends EventDispatcher
    {
        private static var _instance:GridFacade;

        public function GridFacade(lock:SingletonLock, target:IEventDispatcher=null) {
            super(target);
            if(!(lock is SingletonLock)) {
                throw(new Error("GridFacade is a singleton, please do not make foreign instances of it"));
            }
        }

        public static function getInstance():GridFacade {
            if(!_instance) {
                _instance = new GridFacade(new SingletonLock());
            }
            return _instance;
        }
    }
}
class SingletonLock{}

创建一个像这样的可调度事件:

package com.app.events {

    import flash.events.Event;
    import flash.events.IEventDispatcher;

    public class DispatchableEvent extends Event implements IDispatchableEvent {
        protected var _dispatcher:IEventDispatcher;

        public function DispatchableEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
        {
            super(type, bubbles, cancelable);
        }

        public function dispatch():void
        {
            _dispatcher.dispatchEvent(this);
        }
    }
}

然后有一个像这样的自定义GridEvent:

package com.app.events {
    public class GridEvent extends DispatchableEvent {
        public static const SHOW_DATA_IN_GRID:String = "showDataInGrid";
        public var data:Object;
        public function GridEvent(type:String, 
                                  data:ArrayCollection = null, 
                                  bubbles:Boolean=false, 
                                  cancelable:Boolean=false) {
            super(type,bubbles,cancelable);
            _dispatcher = GridFacade.getInstance();
            this.data = data;
        }
    }
}

然后在网格组件的范围内侦听showDataInGrid事件:

...
GridFacade.getInstance().addEventListener(GridEvent.SHOW_DATA_IN_GRID, onShowDataInGrid);
...

protected function onShowDataInGrid(event:GridEvent):void {
    myGrid.dataProvider = event.data;
    // refresh the collection so that the component will display the new data
    event.data.refresh();
    // remember to reset any data specific stuff you may have set in the grid component before doing this.
}

实际更改数据,在您希望的任何类中执行以下操作:

(new GridEvent(
    GridEvent.SHOW_DATA_IN_GRID,
    someCollectionToShowInTheGrid,
)).dispatch();

观看魔术! :)

祝你好运!