jaredhanson / electrolyte

Elegant dependency injection for Node.js.
MIT License
564 stars 61 forks source link

Should Electrolyte load scripts based on require annotations? #8

Closed jsumners closed 10 years ago

jsumners commented 10 years ago

Looking through the Express example, it seems as if Electrolyte will load scripts based on the names given in require annotations. I'm not finding this to be the case. Take a look at this repository -- https://github.com/jsumners/electrolyte-test

Should that repository work as-is? Because I'm getting the following error when I run it:

TypeError: Cannot read property 'message' of undefined
    at module.exports (/private/tmp/04/electrolyte-test/server/config/routes.js:11:29)
    at Factory.instantiate (/private/tmp/04/electrolyte-test/node_modules/electrolyte/lib/patterns/factory.js:15:18)
    at Factory.Component.create (/private/tmp/04/electrolyte-test/node_modules/electrolyte/lib/component.js:29:28)
    at Container.create (/private/tmp/04/electrolyte-test/node_modules/electrolyte/lib/container.js:81:15)
    at Object.<anonymous> (/private/tmp/04/electrolyte-test/server.js:12:18)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

Or am I mistaken in my understanding of how Electrolyte works? Should be creating instances of my components prior to trying to use them as dependencies?

jaredhanson commented 10 years ago

I believe that your problem is this:

exports['@require'] = ['controllers/foo'];
exports['@singleton'] = true;

exports = module.exports = function(fooController) {

The last line is setting exports, thus overwriting your annotations. Move the annotations below and it should work.

If you want to keep the @requires near the factory function, you can do:

var dependencies = ['controllers/foo'];
exports = module.exports = function(fooController) {
//...
}

exports['@require'] = dependencies;
exports['@singleton'] = true;
jsumners commented 10 years ago

Yep. That was it. Man I feel like a dunce. Thank you for the assistance.