Thanks a lot!!!!

On Sat, May 3, 2008 at 3:08 PM, Jim Hayes <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
>
>
>  Apologies, both were additions/modifications to the code that you posted.
>
>  So, add
>
>
>  this.dispatchEvent(new Event("LoadTextFile.dataloaded"));
>
>  to the end of method "onCompleteTextLoad" in your LoadTextFile class,
>
>  and " extends EventDispatcher" to it's definition, to make :
>
>  "public class LoadTextFile extends EventDispatcher"
>
>  (You'll also need to include EventDispatcher in that class)
>
>  It will then dispatch an event when the data is ready.
>
>  The second bit was just modifications to your "controller" class example
> code, in other words adding an event listener to an instance of
> LoadTextFile,
>  and providing a function that gets called when the event occurs(at which
> point the data is available, before that it will be null, as you found).
>
>
>  -----Original Message-----
>  From: flexcoders@yahoogroups.com on behalf of dnk
>
>  Sent: Sat 03/05/2008 21:59
>  To: flexcoders@yahoogroups.com
>  Subject: Re: [flexcoders] Why would this assignment to a MVC model return
> null?
>
>  and which file it should be in, etc....
>
>  On Sat, May 3, 2008 at 1:51 PM, dnk <[EMAIL PROTECTED]> wrote:
>  > Ok, just trying to look through the code sample provided.... a little
>  > confused due to my old code still being in there.....
>  >
>  >
>  >
>  >
>  > On Sat, May 3, 2008 at 12:55 PM, Jim Hayes <[EMAIL PROTECTED]>
> wrote:
>  > >
>  > >
>  > >
>  > >
>  > >
>  > >
>  > > It's because the loading of the text file is asynchronous, and in your
>  > > example
>  > >
>  > >
>  > > var myTxt2:LoadTextFile = new LoadTextFile("file2.txt");
>  > > // populate my model with the results
>  > > myprojectmodel.getInstance().mytext2 = myTxt2.data;
>  > >
>  > > you access it's data variable immediately after instructing it to load
> the
>  > > text.
>  > >
>  > > Instead, you need to wait until that load is done before accessing
> data.
>  > > I'd probably get the loadtextFile class to raise an event when that
>  > > happens, have your main app listen for it.
>  > >
>  > >
>  > > public function onCompleteTextLoad(e:Event):void
>  > > {
>  > > var vars:URLVariables = new URLVariables(e.target.data);
>  > > trace("LOOK HERE: " + vars.thetext);
>  > > this._mydata = vars.thetext;
>  > > trace("from mydata: " + this._mydata);
>  > >
>  > > // If below added, the model is populated
>  > > // myprojectmodel.getInstance().mytext2 = vars.thetext;
>  > >
>  > > this.dispatchEvent(new Event("LoadTextFile.dataloaded"));
>  > >
>  > >
>  > > var myTxt2:LoadTextFile = new LoadTextFile("file2.txt");
>  > > // populate my model with the results
>  > > //myprojectmodel.getInstance().mytext2 = myTxt2.data;
>  > > myTxt2.addEventListener("LoadTextFile.dataloaded", gotData)
>  > >
>  > > private function gotData(event:Event):void
>  > > {
>  > > var myTxt2:LoadTextFile = LoadTextFile(event.target)
>  > >
>  > > myprojectmodel.getInstance().mytext2 = myTxt2.data;
>  > > myTxt2.removeEventListener("LoadTextFile.dataloaded", gotData);
>  > > }
>  > >
>  > > LoadTextFile will have to extend EventDispatcher.
>  > >
>  > > HTH ! (been there myself, when I started!)
>  > >
>  > >
>  > >
>  > > -----Original Message-----
>  > > From: flexcoders@yahoogroups.com on behalf of dnk
>  > > Sent: Sat 03/05/2008 20:22
>  > > To: flexcoders list
>  > > Subject: [flexcoders] Why would this assignment to a MVC model return
> null?
>  > >
>  > > Ok, I am using a easymvc structure (tom bray version), and i have run
>  > > into an issue that is killing me!
>  > >
>  > > I am simply loading in some vars from a text file, and to do so i
>  > > wrote a utility class (to make it reusable).
>  > >
>  > > Now in my class, if I populate my model var, it works fine. But it
>  > > makes that class not very reusable.
>  > >
>  > > So I added a var to store the result, and made a getter method.
>  > >
>  > > Now in my utility class it looks like this:
>  > >
>  > > package myproject.util
>  > > {
>  > > // for text loading
>  > > import flash.events.*;
>  > > import flash.net.*;
>  > >
>  > > public class LoadTextFile
>  > > {
>  > >
>  > > private var _mydata:String;
>  > >
>  > > public function LoadTextFile(myFile:String)
>  > > {
>  > > var req:URLRequest = new URLRequest(myFile);
>  > > var url_loader:URLLoader = new URLLoader(req);
>  > > url_loader.addEventListener(Event.COMPLETE, onCompleteTextLoad);
>  > > url_loader.addEventListener(IOErrorEvent.IO_ERROR, onErrorTextLoad);
>  > > //trace("called in loadTxtFile: " + myFile);
>  > > }
>  > >
>  > > public function onCompleteTextLoad(e:Event):void
>  > > {
>  > > var vars:URLVariables = new URLVariables(e.target.data);
>  > > trace("LOOK HERE: " + vars.thetext);
>  > > this._mydata = vars.thetext;
>  > > trace("from mydata: " + this._mydata);
>  > >
>  > > // If below added, the model is populated
>  > > // myprojectmodel.getInstance().mytext2 = vars.thetext;
>  > >
>  > > }
>  > >
>  > > public function onErrorTextLoad(e:IOErrorEvent):void
>  > > {
>  > > trace("Something wrong! Could not load file.");
>  > > }
>  > >
>  > > public function get data():String
>  > > {
>  > > return this._mydata;
>  > > }
>  > >
>  > > }
>  > > }
>  > >
>  > > The traces in the class come back with the proper data. So the _mydata
>  > > var is populated.
>  > >
>  > > Now from my controller: I would have a function that contained
>  > > something like this:
>  > >
>  > > // new instance
>  > > var myTxt:LoadTextFile = new LoadTextFile("file.txt");
>  > > // populate my model with the results
>  > > myprojectmodel.getInstance().mytext = myTxt.data;
>  > >
>  > > // another instance
>  > > var myTxt2:LoadTextFile = new LoadTextFile("file2.txt");
>  > > // populate my model with the results
>  > > myprojectmodel.getInstance().mytext2 = myTxt2.data;
>  > >
>  > > trace("From Data in controller using getter: " + myTxt.data);
>  > >
>  > > And this trace comes back null! WHich means it obviously never
>  > > populates my model.
>  > >
>  > > Why would this happen?
>  > >
>  > > It seems to be pretty basic code. But I am driving myself insane!!!!
>  > >
>  > > DNK
>  > >
>  > > __________________________________________________________
>  > > This communication is from Primal Pictures Ltd., a company registered
> in
>  > > England and Wales with registration No. 02622298 and registered office:
> 4th
>  > > Floor, Tennyson House, 159-165 Great Portland Street, London, W1W 5PA,
> UK.
>  > > VAT registration No. 648874577.
>  > >
>  > > This e-mail is confidential and may be privileged. It may be read,
> copied
>  > > and used only by the intended recipient. If you have received it in
> error,
>  > > please contact the sender immediately by return e-mail or by
> telephoning
>  > > +44(0)20 7637 1010. Please then delete the e-mail and do not disclose
> its
>  > > contents to any person.
>  > > This email has been scanned for Primal Pictures by the MessageLabs
> Email
>  > > Security System.
>  > > __________________________________________________________
>  > >
>  > >
>  >
>
>  __________________________________________________________
>  This communication is from Primal Pictures Ltd., a company registered in
> England and Wales with registration No. 02622298 and registered office: 4th
> Floor, Tennyson House, 159-165 Great Portland Street, London, W1W 5PA, UK.
> VAT registration No. 648874577.
>
>  This e-mail is confidential and may be privileged. It may be read, copied
> and used only by the intended recipient. If you have received it in error,
> please contact the sender immediately by return e-mail or by telephoning
> +44(0)20 7637 1010. Please then delete the e-mail and do not disclose its
> contents to any person.
>  This email has been scanned for Primal Pictures by the MessageLabs Email
> Security System.
>  __________________________________________________________
>
>  

Reply via email to