PDA

View Full Version : No Return


brock555
07-26-2007, 04:58 AM
I'm trying to return an dictionary, but I also threw an array in there for test and I can't get either one to return. When I place a trace in the get functions it traces absolutely nothing, so I made a tracer function and it traces the array just fine. What the heck am I missing?

package com.fgmarchitects {

import com.fgmarchitects.navigation.data.NavData;
import com.fgmarchitects.navigation.data.LinkData;
import flash.display.Sprite;
import flash.utils.Dictionary;
public class NavTest extends Sprite {

private var _data:NavData;
var _dict:Dictionary;
var _menu:Array;

public function NavTest(){
_dict = new Dictionary();
_menu = new Array();
_data = new NavData("menu.xml")
_dict = _data.getData();
trace(_data.getArray());
//traces nothing
}
}
}


package com.fgmarchitects.navigation.data {

import flash.events.Event;
import flash.events.EventDispatcher;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.utils.Dictionary;
import com.fgmarchitects.navigation.data.NavData;
import com.fgmarchitects.navigation.data.LinkData;

public class NavData extends EventDispatcher {

private var _menuXml:XML;
private var _menuArray:Array;
private var _subArray:Array;
private var _dict:Dictionary;

public function NavData(url:String){
initXML(url);
}


// This function, initXML, takes a single parameter (xmlpath) and is responsible for
// loading and parsing the XML document and converting it into an array of objects.
private function initXML(xmlpath:String):void {
// The XML object which you will use to load and parse the XML navigation file.
_menuXml = new XML();
_menuXml.ignoreWhitespace = true;
var xmlLdr:URLLoader = new URLLoader();
xmlLdr.addEventListener(Event.COMPLETE, completeHandler);
// load the navigation XML file.
xmlLdr.load(new URLRequest(xmlpath));
_dict = new Dictionary();
_menuArray = new Array();
}


public function getData():Dictionary {
return _dict;
}

public function getArray():Array {
trace(_menuArray);
//traces nothing.
return _menuArray;
}

private function completeHandler(event:Event):void {
_menuXml = XML(event.currentTarget.data);
var menuXMLList:XMLList = _menuXml.menu;
var nodeXML:XML;
var i:int = 0;
// for each child node in the XML file (the child nodes here are the main menu navigation items.
for each (nodeXML in menuXMLList) {
var mainLink:LinkData = new LinkData();
mainLink.setName(nodeXML.@name);
mainLink.setHref(nodeXML.@href);
//trace(mainLink.getHref());
mainLink.setImg(nodeXML.@img);
_dict[String(mainLink.getHref())] = mainLink;
// create an empty array for sub-navigation items.
_subArray = new Array();
// for each child node of the main menu items, append the values to our submenu_array.
var subNodeXML:XML;
var p:int = 0;
for each (subNodeXML in nodeXML.children()) {
var subLink:LinkData = new LinkData();
subLink.setName(subNodeXML.@name);
subLink.setHref(subNodeXML.@href);
subLink.setImg(subNodeXML.@img);
_dict[String(subLink.getHref())] = subLink;
//trace(subLink.getHref());
mainLink.addChildLink(subLink);
_subArray.push({caption:subNodeXML.@name, href:subNodeXML.@href, img:subNodeXML.@img});
}
// append each menu items, and it's array of submenu items.
_menuArray.push({caption:nodeXML.@name, href:nodeXML.@href, img:subNodeXML.@img, subnav_array:_subArray});
}
// call the XmlMenu class' initMenu method.
//initMenu(menu_array);
tracer();
}

private function tracer():void {
trace(_menuArray);
//traces objects fine.
}

}
}

panel
07-26-2007, 06:41 AM
getData() & getArray() looks ok. Are you sure you using them after completeHandler has been fired?

oneofhero
07-26-2007, 08:50 AM
I post some problem like this last week. The only thing that I find out about this problem is the trace from getData() and getArray() will first appear to the output rather than the trace for the tracer(). If that is the case, how to make sure completeHandler will complete first before the getData()? I am still trying to get the answer.

senocular
07-26-2007, 11:26 AM
You have to wait for completeHandler to complete before being able to access that data - thats what that handler is for, to indicate when that data is available. It wont be accessible direclty after making a new instance.

oneofhero
07-27-2007, 01:55 AM
Is it the only way to access the data is inside the completeHandler function? Is there some other way to do it? I am trying to do something similar to these twice in my code. And my code will become a completehandler inside another completeHandler. It works but i just wish there is some better way to do it. :confused:

panel
07-27-2007, 04:46 AM
It's the way thing should be done. Why exaclty you don't like this?