RE: [Flashcoders] OOP and XML
thanks Jason, i can see why that is a good way to do things. because an xml file could vary so much in what it contains and how its written, with your classes you don't need to rewrite an xml loading class for each project just a class to deal with the xml once its loaded, so saving on coding time. Date: Thu, 19 Nov 2009 13:13:59 -0500 From: jason.merr...@bankofamerica.com Subject: RE: [Flashcoders] OOP and XML To: flashcoders@chattyfig.figleaf.com David - here is a class I wrote which I use all the time for working with XML. It's a slightly different approach I prefer, where the class listens for the load to complete, and then creates a public XML property of the data you can get once the load is done. Here is the class and the associated custom event: package utils { import events.XMLFileLoaderEvent; import flash.events.Event; import flash.events.EventDispatcher; import flash.net.URLLoader; import flash.net.URLRequest; /** * ... * @author Jason Merrill - Bank of America */ public class XMLFileLoader extends EventDispatcher { public var xml:XML; private var _xmlURL:String; private var _urlLoader:URLLoader; public function XMLFileLoader(xmlURL:String) { _xmlURL = xmlURL; _urlLoader = new URLLoader(new URLRequest(_xmlURL)); _urlLoader.addEventListener(Event.COMPLETE, onXMLLoaded); } private function onXMLLoaded(event:Event):void { xml = new XML(event.target.data); dispatchEvent(new XMLFileLoaderEvent(XMLFileLoaderEvent.LOAD_COMPLETE)); } } } //Custom event package events { /** * ... * @author Jason Merrill - Bank of America */ import flash.events.Event; public class XMLFileLoaderEvent extends Event { public static var LOAD_COMPLETE:String = my_event; public function XMLFileLoaderEvent (type:String, bubbles:Boolean=false, cancelable:Boolean=false) { super(type, bubbles, cancelable); } public override function clone():Event { return new XMLFileLoaderEvent(type, bubbles, cancelable); } public override function toString():String { return formatToString(XMLFileLoaderEvent, type, bubbles, cancelable, eventPhase); } } } Jason Merrill Bank of America Global Learning Learning Performance Soluions Join the Bank of America Flash Platform Community and visit our Instructional Technology Design Blog (note: these are for Bank of America employees only) -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of David Hunter Sent: Thursday, November 19, 2009 11:51 AM To: flashcoders@chattyfig.figleaf.com Subject: RE: [Flashcoders] OOP and XML thanks greg, and nice to know that either way is ok. don't want to start down bad habits already! i've got it working now with a custom event and a little extra help from this as well: http://www.learningactionscript3.com/2008/11/11/passing-arguments-with-e vents/ cheers. Date: Thu, 19 Nov 2009 10:53:18 -0500 Subject: Re: [Flashcoders] OOP and XML From: breakfastcof...@gmail.com To: flashcoders@chattyfig.figleaf.com either way works... its a 6 of one half a dozen of another type situation so you could say dispatchEvent(Event.COMPLETE) from your XMLLoader class and put the data into a public var that your class listening can access. Or you could make a custom event like this dispatchEvent(new XMLEvent(XMLEvent.DATA_LOADED, loader.data)); where you send in the data into a custom event u make hope this makes sense greg On Thu, Nov 19, 2009 at 10:38 AM, David Hunter davehunte...@hotmail.comwrote: thanks Henrik, are you saying that i need to create a custom event class and dispatch an event from that class? otherwise how else could i attach the data to a dispatched event? or do you mean to dispatch an existing event type so when fired the DocumentClass will go into the XMLLoaderClass and grab a public var containing the data? i have never dispatched events before, so i guess i'll have to look into it. i have posted my two classes on pastebin to look at: http://pastebin.com/m4ba8e745 cheers Date: Thu, 19 Nov 2009 13:58:45 +0100 From: he...@henke37
[Flashcoders] OOP and XML
hi, just started out trying OOP. I've done some little things but never a full site, so its my first time attempt using the document class. i made a working demo which creates buttons and loads an image from an array when you click one of the buttons, using a button class and a loader class. the array was defined in the document class, but i'd rather it came from an xml file so it could be updated dynamically. the trouble i am having is returning the xml data from my XMLLoaderClass to the DocumentClass to do something with that data. i have an event listener in the XMLLoaderClass to listen for when the xml file has been loaded, but how do i send that processed data back to the DocumentClass to use? i'm having trouble trying to return the value eg. return xmlData; if i send to the XMLLoaderClass constructor a holder for the data and fill it in the constructor, but then if i trace the holder in the document class it is still empty. if i setup a public method for grabbing the xml data then how do i know it has finished loading eg. myXMLLoaderClass.grabXML(); and isn't called too early? i'll happily post any code but don't want to clog up the email, so if you want it, i'll provide it. i'm pretty sure this is a _very_ basic part of OOP that i am having trouble with or misunderstanding or approaching completely the wrong way, but my copy of Colin Moock's Essential Actionscript 3 (which i'm part way through) is in a box as I have just moved house and i can't find the bit in the pdf version, so i've turned to the list for help. thanks in advance for any help. david _ Have more than one Hotmail account? Link them together to easily access both http://clk.atdmt.com/UKM/go/186394591/direct/01/___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] OOP and XML
Dispatch an event when the data is ready and have the main code listen for the event. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] OOP and XML
thanks Henrik, are you saying that i need to create a custom event class and dispatch an event from that class? otherwise how else could i attach the data to a dispatched event? or do you mean to dispatch an existing event type so when fired the DocumentClass will go into the XMLLoaderClass and grab a public var containing the data? i have never dispatched events before, so i guess i'll have to look into it. i have posted my two classes on pastebin to look at: http://pastebin.com/m4ba8e745 cheers Date: Thu, 19 Nov 2009 13:58:45 +0100 From: he...@henke37.cjb.net To: flashcoders@chattyfig.figleaf.com Subject: Re: [Flashcoders] OOP and XML Dispatch an event when the data is ready and have the main code listen for the event. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders _ Got more than one Hotmail account? Save time by linking them together http://clk.atdmt.com/UKM/go/186394591/direct/01/___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] OOP and XML
either way works... its a 6 of one half a dozen of another type situation so you could say dispatchEvent(Event.COMPLETE) from your XMLLoader class and put the data into a public var that your class listening can access. Or you could make a custom event like this dispatchEvent(new XMLEvent(XMLEvent.DATA_LOADED, loader.data)); where you send in the data into a custom event u make hope this makes sense greg On Thu, Nov 19, 2009 at 10:38 AM, David Hunter davehunte...@hotmail.comwrote: thanks Henrik, are you saying that i need to create a custom event class and dispatch an event from that class? otherwise how else could i attach the data to a dispatched event? or do you mean to dispatch an existing event type so when fired the DocumentClass will go into the XMLLoaderClass and grab a public var containing the data? i have never dispatched events before, so i guess i'll have to look into it. i have posted my two classes on pastebin to look at: http://pastebin.com/m4ba8e745 cheers Date: Thu, 19 Nov 2009 13:58:45 +0100 From: he...@henke37.cjb.net To: flashcoders@chattyfig.figleaf.com Subject: Re: [Flashcoders] OOP and XML Dispatch an event when the data is ready and have the main code listen for the event. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders _ Got more than one Hotmail account? Save time by linking them together http://clk.atdmt.com/UKM/go/186394591/direct/01/___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] OOP and XML
thanks greg, and nice to know that either way is ok. don't want to start down bad habits already! i've got it working now with a custom event and a little extra help from this as well: http://www.learningactionscript3.com/2008/11/11/passing-arguments-with-events/ cheers. Date: Thu, 19 Nov 2009 10:53:18 -0500 Subject: Re: [Flashcoders] OOP and XML From: breakfastcof...@gmail.com To: flashcoders@chattyfig.figleaf.com either way works... its a 6 of one half a dozen of another type situation so you could say dispatchEvent(Event.COMPLETE) from your XMLLoader class and put the data into a public var that your class listening can access. Or you could make a custom event like this dispatchEvent(new XMLEvent(XMLEvent.DATA_LOADED, loader.data)); where you send in the data into a custom event u make hope this makes sense greg On Thu, Nov 19, 2009 at 10:38 AM, David Hunter davehunte...@hotmail.comwrote: thanks Henrik, are you saying that i need to create a custom event class and dispatch an event from that class? otherwise how else could i attach the data to a dispatched event? or do you mean to dispatch an existing event type so when fired the DocumentClass will go into the XMLLoaderClass and grab a public var containing the data? i have never dispatched events before, so i guess i'll have to look into it. i have posted my two classes on pastebin to look at: http://pastebin.com/m4ba8e745 cheers Date: Thu, 19 Nov 2009 13:58:45 +0100 From: he...@henke37.cjb.net To: flashcoders@chattyfig.figleaf.com Subject: Re: [Flashcoders] OOP and XML Dispatch an event when the data is ready and have the main code listen for the event. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders _ Got more than one Hotmail account? Save time by linking them together http://clk.atdmt.com/UKM/go/186394591/direct/01/___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders _ Add your Gmail and Yahoo! Mail email accounts into Hotmail - it's easy http://clk.atdmt.com/UKM/go/186394592/direct/01/___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] OOP and XML
while your on the subject of bad habits, here's a list of coding conventions and best practices to use as a reference. http://opensource.adobe.com/wiki/display/flexsdk/Coding+Conventions I still haven't looked at the whole list yet and I'm not sure how relevant it all is, but its a handy link to check out. greg On Thu, Nov 19, 2009 at 11:50 AM, David Hunter davehunte...@hotmail.comwrote: thanks greg, and nice to know that either way is ok. don't want to start down bad habits already! i've got it working now with a custom event and a little extra help from this as well: http://www.learningactionscript3.com/2008/11/11/passing-arguments-with-events/ cheers. Date: Thu, 19 Nov 2009 10:53:18 -0500 Subject: Re: [Flashcoders] OOP and XML From: breakfastcof...@gmail.com To: flashcoders@chattyfig.figleaf.com either way works... its a 6 of one half a dozen of another type situation so you could say dispatchEvent(Event.COMPLETE) from your XMLLoader class and put the data into a public var that your class listening can access. Or you could make a custom event like this dispatchEvent(new XMLEvent(XMLEvent.DATA_LOADED, loader.data)); where you send in the data into a custom event u make hope this makes sense greg On Thu, Nov 19, 2009 at 10:38 AM, David Hunter davehunte...@hotmail.com wrote: thanks Henrik, are you saying that i need to create a custom event class and dispatch an event from that class? otherwise how else could i attach the data to a dispatched event? or do you mean to dispatch an existing event type so when fired the DocumentClass will go into the XMLLoaderClass and grab a public var containing the data? i have never dispatched events before, so i guess i'll have to look into it. i have posted my two classes on pastebin to look at: http://pastebin.com/m4ba8e745 cheers Date: Thu, 19 Nov 2009 13:58:45 +0100 From: he...@henke37.cjb.net To: flashcoders@chattyfig.figleaf.com Subject: Re: [Flashcoders] OOP and XML Dispatch an event when the data is ready and have the main code listen for the event. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders _ Got more than one Hotmail account? Save time by linking them together http://clk.atdmt.com/UKM/go/186394591/direct/01/___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders _ Add your Gmail and Yahoo! Mail email accounts into Hotmail - it's easy http://clk.atdmt.com/UKM/go/186394592/direct/01/___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] OOP and XML
David - here is a class I wrote which I use all the time for working with XML. It's a slightly different approach I prefer, where the class listens for the load to complete, and then creates a public XML property of the data you can get once the load is done. Here is the class and the associated custom event: package utils { import events.XMLFileLoaderEvent; import flash.events.Event; import flash.events.EventDispatcher; import flash.net.URLLoader; import flash.net.URLRequest; /** * ... * @author Jason Merrill - Bank of America */ public class XMLFileLoader extends EventDispatcher { public var xml:XML; private var _xmlURL:String; private var _urlLoader:URLLoader; public function XMLFileLoader(xmlURL:String) { _xmlURL = xmlURL; _urlLoader = new URLLoader(new URLRequest(_xmlURL)); _urlLoader.addEventListener(Event.COMPLETE, onXMLLoaded); } private function onXMLLoaded(event:Event):void { xml = new XML(event.target.data); dispatchEvent(new XMLFileLoaderEvent(XMLFileLoaderEvent.LOAD_COMPLETE)); } } } //Custom event package events { /** * ... * @author Jason Merrill - Bank of America */ import flash.events.Event; public class XMLFileLoaderEvent extends Event { public static var LOAD_COMPLETE:String = my_event; public function XMLFileLoaderEvent (type:String, bubbles:Boolean=false, cancelable:Boolean=false) { super(type, bubbles, cancelable); } public override function clone():Event { return new XMLFileLoaderEvent(type, bubbles, cancelable); } public override function toString():String { return formatToString(XMLFileLoaderEvent, type, bubbles, cancelable, eventPhase); } } } Jason Merrill Bank of America Global Learning Learning Performance Soluions Join the Bank of America Flash Platform Community and visit our Instructional Technology Design Blog (note: these are for Bank of America employees only) -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of David Hunter Sent: Thursday, November 19, 2009 11:51 AM To: flashcoders@chattyfig.figleaf.com Subject: RE: [Flashcoders] OOP and XML thanks greg, and nice to know that either way is ok. don't want to start down bad habits already! i've got it working now with a custom event and a little extra help from this as well: http://www.learningactionscript3.com/2008/11/11/passing-arguments-with-e vents/ cheers. Date: Thu, 19 Nov 2009 10:53:18 -0500 Subject: Re: [Flashcoders] OOP and XML From: breakfastcof...@gmail.com To: flashcoders@chattyfig.figleaf.com either way works... its a 6 of one half a dozen of another type situation so you could say dispatchEvent(Event.COMPLETE) from your XMLLoader class and put the data into a public var that your class listening can access. Or you could make a custom event like this dispatchEvent(new XMLEvent(XMLEvent.DATA_LOADED, loader.data)); where you send in the data into a custom event u make hope this makes sense greg On Thu, Nov 19, 2009 at 10:38 AM, David Hunter davehunte...@hotmail.comwrote: thanks Henrik, are you saying that i need to create a custom event class and dispatch an event from that class? otherwise how else could i attach the data to a dispatched event? or do you mean to dispatch an existing event type so when fired the DocumentClass will go into the XMLLoaderClass and grab a public var containing the data? i have never dispatched events before, so i guess i'll have to look into it. i have posted my two classes on pastebin to look at: http://pastebin.com/m4ba8e745 cheers Date: Thu, 19 Nov 2009 13:58:45 +0100 From: he...@henke37.cjb.net To: flashcoders@chattyfig.figleaf.com Subject: Re: [Flashcoders] OOP and XML Dispatch an event when the data is ready and have the main code listen for the event. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders _ Got more than one Hotmail account? Save time by linking them together