Pergunta

I'm new to Node and coming from a C# background one of the main things that I'm looking to figure out with Node is dependency injection. I understand that wire.js has that capability and I've read everything I can find on it and I've even downloaded the Html "Hello World" example. However, I'm still unable to get it to work properly.

Here's what I did to try to get it to work:

  1. I pulled the hello-wired.js and hello-wired-spec.js files into my Node project.
  2. I pulled in the wire folder from /js/wire from the sample app to /node-modules/wire in my Node app.
  3. I removed the line of code in hello-world.js in the constructor since I won't have an Html node and I made the constructor parameterless. I then replaced the line in sayHello to use console.log() since I won't have InnerHtml.
  4. I created a test action as


app.get('/testwired', function (req, res) {
    require('wire!hello-wired-spec', function (spec) {
        console.log(spec);
        res.send(spec.sayHello("this is a test"));
    });
});


The error that I'm getting is that it can't find the module wire!hello-wired-spec. I assume that means that I haven't configured wire.js to know where to get my spec, but I can't figure out how. I also don't know if I'm supposed to use a callback for this or not.

Any help is greatly appreciated.

Foi útil?

Solução

The syntax "wire!hello-wired-spec" is specific to AMD loaders (it uses wire as an AMD plugin), and AMD isn't understood by Node's require. So, in node, you'll use wire programmatically, require()ing it like any other other lib. Other than that, though, it will work in basically the same way: you'll feed it a wire spec module, and it will return a promise that will resolve once it has wired it for now.

Here's a simplest-possible "hello wire" written for node. The main.js require()s wire, then feeds the wiring spec (in spec.js) to it. wire() returns a promise that resolves with the fully wired context. There's not much interesting in the wire spec right now, but that's where you come in :)

Hopefully, that's enough to get you up and running with wire in node, but feel free to follow up over at the cujojs google group if you have other questions!

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top