"On demand" requiring / includes

9 views
Skip to first unread message

JoshK

unread,
Sep 9, 2010, 11:38:04 AM9/9/10
to nodejs
I want a directory structure like

./lib/obj/obj-1.js
./lib/obj/obj-2.js
./lib/obj/obj-3.js
...
./lib/obj/obj-n.js

I wish to load these "on demand" so to speak. Each of these is setup
with an identically called constructor as follows:

function Obj()
{
// Note that these values are unique to the obj, so obj-1 has
different values then obj-2
this.name = "Joe";
this.age = 32;
}

I would like to do something as follows in my "main" file:

function Obj(num)
{
var o = require("./lib/obj/obj-" + num + ".js");
return o;
}

var obj1 = new Obj("1");
var obj2 = new Obj("2");
.....
var obj-n = new Obj("n");

While this seems to work, it breaks when I start inheriting
EventEmitter in the Obj's (./lib/obj/obj-n.js). It then says it has no
method "addListener".

Thanks,

-Josh

mscdex

unread,
Sep 9, 2010, 12:13:34 PM9/9/10
to nodejs
On Sep 9, 11:38 am, JoshK <josh.k...@gmail.com> wrote:
> While this seems to work, it breaks when I start inheriting
> EventEmitter in the Obj's (./lib/obj/obj-n.js). It then says it has no
> method "addListener".

Gist some code and where your error is pointing to.

Joshua Kehn

unread,
Sep 9, 2010, 12:24:47 PM9/9/10
to nod...@googlegroups.com
Due to the setup, I instead created a repo for it. Clone it and run `node example.js`.

You should receive results similar to the error.log file.

http://github.com/joshkehn/Node-Issue

Thanks,

-Josh
____________________________________
Joshua Kehn | Josh...@gmail.com
http://joshuakehn.com

> --
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.
>

Joshua Kehn

unread,
Sep 9, 2010, 2:47:31 PM9/9/10
to nod...@googlegroups.com
Got it working.

Essentially I created a generic "Obj" class which extends EventEmitter, then I rewrote the load function as follows:

function getObj(id)
{
var o = require("./lib/obj/obj"+id+".js");
return new o();
}

Regards,

-Josh
____________________________________
Joshua Kehn | Josh...@gmail.com
http://joshuakehn.com

On Sep 9, 2010, at 12:13 PM, mscdex wrote:

mscdex

unread,
Sep 9, 2010, 2:57:24 PM9/9/10
to nodejs
On Sep 9, 12:24 pm, Joshua Kehn <josh.k...@gmail.com> wrote:
> Due to the setup, I instead created a repo for it. Clone it and run `node example.js`.
>
> You should receive results similar to the error.log file.
>
> http://github.com/joshkehn/Node-Issue
>
> Thanks,
>
> -Josh

The reason is that the 'new' operator is creating a new instance of
the Digger object/function and not the value it's returning as you're
expecting. Instead, you can set the value of Digger(1) to a separate
variable and then set the 'digger' variable to a new instance of the
separate variable. Example:

Before:
var digger = new Digger(1);

After:
var dig = new Digger(1),
digger = new dig(); // Add any arguments to Obj1's constructor here

mscdex

unread,
Sep 9, 2010, 2:59:37 PM9/9/10
to nodejs
On Sep 9, 2:57 pm, mscdex <msc...@gmail.com> wrote:
> [...]
> After:
> var dig = new Digger(1),
>     digger = new dig(); // Add any arguments to Obj1's constructor here

Oops, that first line should be: var dig = Digger(1),
Reply all
Reply to author
Forward
0 new messages