Where must be custom logic in pureMVC (as3)?

98 Views Asked by At

I tried to write small as3 program based on framework pureMVC. I understood basic principles of it, but I can't understand, where I must place custom logic. For example, I must load 10 images. I created command, that init Proxy.

package app.controller 
{
    import app.model.GalleryProxy;
    import dicts.Constants;
    import org.puremvc.interfaces.INotification;

    public class LoadFilesCommand extends BaseCommand
    {
        public function LoadFilesCommand() { }

        override public function execute(note:INotification):void
        {
            facade.registerProxy(new GalleryProxy(Constants.FILES_LIST));
        }
    }
}

And Proxy is:

package app.model 
{
    import dicts.Constants;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.events.ErrorEvent;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.net.URLRequest;
    import org.puremvc.interfaces.IProxy;
    import org.puremvc.patterns.proxy.Proxy;

    public class GalleryProxy extends Proxy implements IProxy
    {
        public function GalleryProxy(list:Vector.<String>)
        {
            super(Constants.PROXY_GALLERY);
            _fileList = list;
            _total = _fileList.length;
            load();
        }

        public function get currentImage():Bitmap
        {
            return _images[_index];
        }

        //--------------------------------------------------------------------------
        // PRIVATE SECTION 
        //--------------------------------------------------------------------------
        private var _fileList:Vector.<String>;
        private var _total:uint;
        private var _loaded:uint = 0;
        private var _images:Array = [];
        private var _index:int;

        private function load():void
        {
            var loader:Loader;
            for (var i:int = 0; i < _total; i++)
            {
                loader = new Loader();
                loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoadHandler);
                loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
                loader.load(new URLRequest(_fileList[i]));
            }
        }

        private function imageLoadHandler(event:Event):void
        {
            var info:LoaderInfo = LoaderInfo(event.currentTarget);
            _images[Constants.FILES_LIST.indexOf(info.url)] = info.content;
            info.removeEventListener(Event.COMPLETE, imageLoadHandler);
            info.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler);
            _loaded++;
            if (_loaded >= _total)
                sendNotification(Constants.COMMAND_SHOW_MAIN);
        }

        private function errorHandler(event:ErrorEvent):void
        {
            throw new Error("bad link or internet disconnect");
        }
    }
}

Now my Proxy is loading images independently (functions load() and imageLoadHandler) Is it correct? Or I must move this logic to Command class? Or I must create some LoadService.as, which will contains this logic? What is the correct variant for pureMVC?

1

There are 1 best solutions below

0
On

Do you want to load your 10 images on application startup? If not, make load() public and call it from a Mediator, responding to a UI event.

If so, what you have will work fine. One alternative would be writing GalleryProxy so it doesn't call load() in the constructor - instead, you could have the Command register the proxy, load the image list, and call proxy.load(images[i]) in a loop.