Welcome to javascript, asynchronous events, and functional programming.

On Tue, Nov 10, 2009 at 10:30 AM, jiggliemon <[email protected]> wrote:

>
> I've also been working on this issue:
>
> http://groups.google.com/group/mootools-users/browse_thread/thread/3042589c84611861
>
>
> what you have there is:
> console.log('Fire 1')
> file1.js:
> var foo = bla;
> var bar = bla;
> var myScript = new Asset.javascript('file1.js', {
>            onload: function(){
>                 console.log('Fire 2');
>                myfunc();
>            }
>  });
>
> console.log('Fire 3');
> myfunc(foo);
>
>
> In which it will fire in this order:
> Fire 1
> Fire 3
> Fire 2
>
> Meaning you're calling myfunc(foo) before your onload has been
> initialized/fired.
>
> Test it.  I dare you!
>
> When i emailed Mr.newton about this little mishap and how to manuver
> around the synchronous/asynchronous issue, he wrote (and i quote) :
>
> ---------------------------------------
>
> see highlighted lines here:
>
> http://paste.mootools.net/f2301d75e
>
> the problem is you are dealing with an asynchronous event. the
> javascript order here is:
>
> make script file
> inject in doc.head and *start* to load
> return this.comments
> script loads
> execute onload event
> define this.comments
>
> The only way to do this is to make the whole method have a callback,
> like so:
>
> http://paste.mootools.net/f401cfb7c
>
> This approach has a lot of negatives, the most notable being that your
> Argo class instances can't count on having access to this.comments
> immediately.
>
> To solve this problem in a more generic fashion you would want to look
> at something like the MooTools Depender loader, or maybe the $uses
> loader.
> - Arron Newton
> -------------------------------------------------
>
> On Nov 10, 5:20 am, electronbender <[email protected]> wrote:
> > I am calling myfunc inside the onload function.
> >
> > On Nov 10, 1:44 pm, batman42ca <[email protected]> wrote:
> >
> > > Asset is asynchronous. This means that when you call myfunc(foo)
> > > outside the call to Asset.javascript, there is no guarantee that
> > > file1.js has loaded yet.
> >
> > > When you call myfunc() inside the onload() function, that is where
> > > it's guaranteed that file1.js has been loaded.
>

Reply via email to