Re: Listener for Load() method
Expand Messages
- Hi Ben,
Sorry I didn't write sooner. I haven't been able to dedicate as much time to Ajile since joining Palm a few months back; webOS keeps me pretty busy :-)
Try the following:
1. Add a listener for any globally accessible named value defined by the script you're loading. Based on your description, this would be a function name i.e.:
Ajile.AddImportListener ("window.BensFunction", function (x){ alert ("Loaded "+x+"!"); } );
2. Load the target script and use the Load directive's source parameter (third) to trigger listener notification i.e.:
Load ("", null, "Import ('window');");
This will trigger your listener once BensFunction becomes available.
An explanation for this recommendation:
Ajile is designed to support listening for data rather than resource availability. Using Ajile.AddImportListener (...) you can listen for named items that exist in the global scope. This makes it possible to receive notification not only at the moment the resource that defines your target data has completed loading, but anytime after as long as the data is named in the global scope.
In Step 1 of the above recommended solution a listener is added for a function named "BensFunction" that's added to the global scope by BensStuff.js. The listener is set for "window.BensFunction" to work around a bug in how Ajile handles listening to single-level namespaced items like "BensFunction". To workaround that bug specify any globally named item that references the global scope (i.e. self, top, window) as the parent namespace of the target item (i.e. "window.BensFunction").
In Step 2 Ajile's Load directive is used to simultaneously load an arbitrary script and execute arbitrary code. Load can be used to load script files and/or execute code in a single call. In this case we use it to load BensStuff.js as well as to trigger Ajile's listener notifications. The "Import ('window');" code jump starts Ajile's internal listener handling by importing an already available globally named item (i.e. window). The jump start isn't necessary when using Import, ImportAs, or Include because the targeted scripts use Ajile's API so the system is implicitly aware of targeted data's availability.
I hope this recommendation and explanation helps you. If not I'm happy to elaborate or try a different approach.
For an overview of Ajile's API and links to it's API docs see:
For details on Ajile.AddImportListener() and Load() see:
Best of luck and thanks for using Ajile Ben!
Mike
--- In ajile@yahoogroups.com, "Ben McClure" <digitalempire@...> wrote:
>
> I'm pretty surprised that nobody else has this issue or has found a workaround. It seems like such a basic requirement that, since the files are loaded in the background, there should be some sort of event fired when the loading is complete. How else is it recommended to know when to call functions from within that loaded file?
>
> --- In ajile@yahoogroups.com, "Ben McClure" <digitalempire@> wrote:
> >
> > I cannot find a good way to determine when an external script loaded with Load() is actually loaded... so I think I'll have to dump the use of Ajile for loading my files until I figure it out.
> >
> > It seems like great software and I still plan on using it for my namespaces and class loading, but there are a couple things I'm having trouble getting past:
> >
> > 1. Namespaces are not hierarchal as they should be. Setting a namespace to load from directory ('/' separator) should take effect for all sub-namespaces defined afterward unless explicitely set... this doesn't work and it always defaults back to '.' notation instead.
> >
> > 2. There is no way to tell when files loaded with Load() are ready for use. If there is a way, I am having a huge amount of trouble finding it--most of my weekend went toward figuring out how to best use Ajile to load my Javascript... and the entire weekend's work didn't really get me anywhere.
> >
>
Your message has been successfully submitted and would be delivered to recipients shortly.