1 Reply Latest reply on Dec 17, 2007 11:21 PM by Gregory Lafrance

    Dynamic photo album

    Sefi Ninio
      Hello.

      I am trying to create a dynamic photo album.

      private var xmlString:URLRequest;
      private var xmlLoader:URLLoader;
      private var index:int;
      private var pages:Array;

      private function init():void {
      xmlString = new URLRequest("gallery.xml");
      xmlLoader = new URLLoader(xmlString);
      index = 2;
      pages = new Array();
      xmlLoader.addEventListener(Event.COMPLETE, initData);
      }

      private function initData(event:Event):void {
      var albumsXML:XML = XML(xmlLoader.data);
      var entries:XMLListCollection = new XMLListCollection(albumsXML.children());
      for (var i:int = 0; i < entries.length; i++) {
      var albumName:String = entries.getItemAt(i).attribute("location");
      //trace("album name: " + albumName);
      var imagesEntry:XMLListCollection = new XMLListCollection(entries.getItemAt(i).children());
      for (var j:int = 0; j < imagesEntry.length; j++) {
      var imageSource:String = imagesEntry.getItemAt(j).attribute("source");
      //trace("image source: " + imageSource);

      addPage(albumName+imageSource, index++);
      }

      }
      }

      private function addPage(imageSource:String, index:int):void {
      var page:PortratePage = new PortratePage();
      page.addEventListener(FlexEvent.CREATION_COMPLETE, onPageCreated);
      var pageAttributes:ArrayCollection = new ArrayCollection();
      pageAttributes.addItem(imageSource);
      pageAttributes.addItem(index);

      pages[page] = pageAttributes;
      trace(pages[page] + "," + pages.length);
      }

      private function onPageCreated (event:FlexEvent):void {
      //do stuff
      }

      XML data is loaded just fine and the addPage function gets called.
      In it, I initialize a new page (external PortratePage.mxml), but the handling onPageCreated function is never called.
      I was told that initializing mxml instances is done asynchronously, that's why I need to define a CREATION_COMPLETE event but for some reason it does not work.

      Anyone has any idea why?
      BTW: is there a way to make external mxml objects initialization to be synchronously?

      Thanks
      Sefi