You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
module.exports.needs= ['express'];// What your package needs
28
+
module.exports.fn=function($){// Dependencies are injected through $
29
29
var app =$.express();
30
30
31
31
// Add your routes and configuration here
32
-
33
-
return app;
32
+
33
+
return app;
34
34
}
35
35
```
36
36
```js
37
37
// config.js
38
38
module.exports= {
39
-
url:__dirname, // Base URL
40
-
packages: { // Packages to be injected
41
-
'server':'./server',
42
-
'express':'express'
43
-
}
39
+
'server':require('./server'), // Provide packages
40
+
'express':require('express')
44
41
}
45
42
```
46
43
47
44
## Why?
48
45
49
-
Dependency injection shouldn't be complicated. `nwire.js` encourages loosely coupled functionality and simplifies the process of isolating your code for testing.
46
+
Dependency injection shouldn't be complicated. `nwire` encourages loosely coupled functionality and simplifies the process of isolating your code for testing.
50
47
51
-
## Creating packages
48
+
## Creating the container
52
49
53
-
### Package definition
50
+
You must feed `nwire` a configuration object containing the packages you wish to provide for injection.
51
+
52
+
Consider this sample configuration object.
53
+
```js
54
+
// config.js
55
+
module.exports= {
56
+
'app':require('./server'),
57
+
'redis-db':require('./db'),
58
+
'express':require('express'),
59
+
'morgan':require('morgan'),
60
+
'passport':require('passport')
61
+
};
62
+
```
63
+
64
+
Here we can see that the packages `app`, `redis-db`, `express`, `morgan`, and `passport` are registered and are ready to be injected in packages that need them. `nwire` will then inject all other four packages through the `imports` parameter for packages that contain the properties `fn` and `needs`.
This package resolves an object that exposes two functions: `login` and `logout`. The resolved object is then injected in other packages that require it through the `needs` property.
96
+
This package returns an object that exposes two functions: `login` and `logout`. The returned object is then injected in other packages that require it through the `needs` property.
74
97
75
98
```js
76
99
// server.js
77
100
module.exports.needs= ['auth'];
78
-
module.exports.fn=function(imports) { // You can use $ for short
79
-
var auth =imports.auth; // The auth module is injected
If the `fn`property is not provided, nwire.js will not perform any dependency injection. If the `needs` property is not provided, the `imports` parameter will be empty.
107
+
If the `fn`and `needs` properties are not provided, `nwire` will not perform any dependency injection.
86
108
87
-
### Package discovery
109
+
## Running the test suite
88
110
89
-
In order to perform dependency injection, you must feed nwire.js a configuration object containing the `url` and `packages` properties.
111
+
```
112
+
$npminstall
113
+
$npmtest
114
+
```
90
115
91
-
The `url` property allows nwire.js to resolve packages without needing their absolute paths. In most configurations, assigning `__dirname` to the `url` property will do. If this property is not provided, nwire.js will attempt to resolve modules from within its own directory.
116
+
## Breaking changes from v0.1
92
117
93
-
The `packages` property assigns a name and location for every package. It must contain an object where property names define package names and property values are corresponding locations.
118
+
Release `v0.2` did away with string declarations for `config.js` files. This is to allow `nwire` applications to work with bundlers like Browserify and `system.js`. If your `config.js` file looked like this:
94
119
95
-
Consider this sample configuration object.
96
-
```js
97
-
// config.js
98
-
var path =require('path');
120
+
```javascript
99
121
module.exports= {
100
-
url:path.join(__dirname, 'src'),
122
+
url:__dirname,
101
123
packages: {
102
-
'app':'./server',
103
-
'database':'./db',
104
-
'express':'express',
105
-
'morgan':'morgan',
106
-
'passport':'passport'
124
+
'app':'./app'
107
125
}
108
-
};
126
+
}
109
127
```
110
128
111
-
Here we can see that the packages `app`, `database`, `express`, `morgan`, and `passport` are registered and are ready to be injected in packages that need them. Assuming that the `app` package looks like the following code, nwire.js will inject all other four packages through the `imports` parameter.
129
+
You will now need to use CommonJS (or equivalent) to load your application.
// import now contains four properties each named after the injected packages
118
-
varapp = import.express();
131
+
```javascript
132
+
module.exports= {
133
+
'app':require('./app')
119
134
}
120
135
```
121
136
137
+
Also, packages are now properties of the container returned by `nwire` rather than living under a `packages` object.
138
+
139
+
```javascript
140
+
wire({ /*...config...*/}, function(err, app) {
141
+
// app.packages.server.bootstrap(3000);
142
+
app.server.bootstrap(3000);
143
+
});
144
+
```
145
+
122
146
## Suggestions and questions
123
147
124
-
If you have any suggestions or questions regarding this project, please open an issue. If you feel that you have a feature that would be useful to add, fork it and open a pull request.
148
+
If you have any suggestions or questions regarding this project, please open an issue. If you feel that you have a feature that would be useful to add, fork it and open a pull request.
0 commit comments