Second that. Failing that check out commonjs's require spec which
allows you to do just what you ate asking for but it does require all
you stuff to be written with the commonjs's spec in mind. It's not
that hard to patch up existing code either.
Debugging is the hardest part with these dynamic loaders. Errors
easily get buried or lost so tread with care.
Sent from my iPhone
On 7 Apr 2010, at 17:47, Aaron Newton <[email protected]> wrote:
I think you'd be better off using Depender.
On Wed, Apr 7, 2010 at 6:24 AM, batman42ca <[email protected]>
wrote:
> If you had the external file "foo.js" and inside "foo.js" was a
class called
> "bar", you want, the command "new bar()" to load "foo.js" in a
script tag
> and initiate the class "bar", is this right?
Yes and no. More likely foo.js would contain a class called Foo. So
given the name of the class, I know the name of the file.
> to intercept the call to "new"
I'm wondering if instead of that, I could create a new function let's
just call it "dnew" for "Dependant New" or something like that. Not a
great name but the only other short name I can think of is "newd"
So I instead of this:
myObj = new MyClass(param1,{
option1 : 1,
option2 : 2
});
I would do this
myObj = dnew(MyClass(param1,{
option1: 1,
option2 :2
)));
then dnew checks for the existance of MyClass, if it doesn't already
exist, it loads MyClass.js, then regardless it calls "new" although
I'm not quite sure how I could capture the input to "dnew" and use it
to call "new". I see the need for eval() in my future.
On Apr 6, 11:18 pm, "Matthew Hazlett" <[email protected]> wrote:
> If you had the external file "foo.js" and inside "foo.js" was a
class called
> "bar", you want, the command "new bar()" to load "foo.js" in a
script tag
> and initiate the class "bar", is this right?
>
> Well, the only way that I know of that you could accomplish such a
thing in
> pure JS is to intercept the call to "new" determine what file the
class
> desired is located in. But, short of joining the Mozilla dev team
and
> writing that in to the next version of JavaScript I would venture
to say you
> are out of luck. As far as I know, there is no hook or event that
gets
> triggered when you issue the command "new".
>
> However, thinking out loud a minute you might be able to trap for
an error
> then use a async ajax query that calls a php function that scans a
directory
> of files for the class you want. Returns the file name and
dynamically
> loads it, then tries to create the class. (takes a deep breath,
then falls
> over from exhaustion)
--
To unsubscribe, reply using "remove me" as the subject.