Skip to content

Commit 9b5e37a

Browse files
jaumardjaumard
jaumard
authored and
jaumard
committed
Simplify usage
1 parent e04e70a commit 9b5e37a

File tree

8 files changed

+100
-51
lines changed

8 files changed

+100
-51
lines changed

README.md

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,54 @@
22
Load the models, controllers, services, policies and config from your Sails hooks into the main app and extend it on your main sails server if you need
33

44
### USAGE
5-
Look the example folder.
5+
Look the [example folder](https://github.com/jaumard/sails-util-mvcsloader/tree/master/example/sails-hook-echo) for files structure.
66

77
Install it on your installable hook folder :
88

99
```
1010
npm install --save sails-util-mvcsloader
1111
```
1212

13-
Then use it like this in your hook's index.js :
13+
Then use it like this in your hook's index.js :
1414

15-
```
16-
module.exports = function(sails) {
17-
var hookLoader = require('sails-util-mvcsloader')(sails);
18-
hookLoader.injectAll({
19-
policies : __dirname + '/policies',// Path to your hook's policies
20-
config : __dirname + '/config'// Path to your hook's config
21-
});
22-
return {
23-
initialize: function(cb) {
24-
hookLoader.injectAll({
25-
controllers: __dirname+'/controllers', // Path to your hook's controllers
26-
models: __dirname+'/models', // Path to your hook's models
27-
services: __dirname+'/services' // Path to your hook's services
28-
}, function(err) {
29-
return cb();
30-
});
31-
}
15+
16+
module.exports = function(sails) {
17+
var loader = require("sails-util-mvcsloader")(sails);
18+
loader.configure(); // Load policies under ./api/policies and config under ./config
19+
20+
/*
21+
OR if you want to set custom path :
22+
loader.configure({
23+
policies: __dirname + '/api/policies',// Path to your hook's policies
24+
config: __dirname + '/config'// Path to your hook's config
25+
});
26+
*/
27+
28+
return {
29+
initialize: function (next) {
30+
/*
31+
Load models under ./api/models
32+
Load controllers under ./api/controllers
33+
Load services under ./api/services
34+
*/
35+
loader.adapt(function (err) {
36+
return next(err);
37+
});
38+
39+
/*
40+
OR if you want to set custom path for your files :
41+
loader.adapt(function (err) {
42+
return next(err);
43+
},{
44+
controllers: __dirname + '/controllers', // Path to your hook's controllers
45+
models: __dirname + '/models', // Path to your hook's models
46+
services: __dirname + '/services' // Path to your hook's services
47+
});
48+
*/
49+
}
50+
};
3251
}
33-
}
34-
```
52+
3553

3654
### USED BY
3755
[sails-hook-passport](https://github.com/jaumard/sails-hook-passport)

example/sails-hook-echo/index.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,32 @@
44

55
module.exports = function (sails) {
66
var loader = require("sails-util-mvcsloader")(sails);
7-
loader.injectAll({
8-
policies: __dirname + '/policies',// Path to your hook's policies
9-
config: __dirname + '/config'// Path to your hook's config
10-
});
7+
loader.configure(); // Load policies and config
118

9+
/*
10+
OR if you want to set custom path :
11+
loader.configure({
12+
policies: __dirname + '/api/policies',// Path to your hook's policies
13+
config: __dirname + '/config'// Path to your hook's config
14+
});
15+
*/
1216

1317
return {
14-
// Run when sails loads-- be sure and call `next()`.
1518
initialize: function (next) {
16-
loader.injectAll({
17-
controllers: __dirname + '/controllers', // Path to your hook's controllers
18-
models: __dirname + '/models', // Path to your hook's models
19-
services: __dirname + '/services' // Path to your hook's services
20-
}, function (err) {
19+
loader.adapt(function (err) {
2120
return next(err);
2221
});
22+
23+
/*
24+
OR if you want to set custom path :
25+
loader.adapt(function (err) {
26+
return next(err);
27+
},{
28+
controllers: __dirname + '/api/controllers', // Path to your hook's controllers
29+
models: __dirname + '/api/models', // Path to your hook's models
30+
services: __dirname + '/api/services' // Path to your hook's services
31+
});
32+
*/
2333
}
2434
};
2535
};

index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,27 @@ module.exports = function (sails) {
2525
require(__dirname + "/libs/services")(sails, dir, cb);
2626
},
2727

28+
configure: function (dir) {
29+
if (!dir) {
30+
dir = {
31+
config: __dirname + '/../../config',
32+
policies: __dirname + '/../../api/policies'
33+
};
34+
}
35+
this.injectAll(dir);
36+
},
37+
38+
adapt: function (next, dir) {
39+
if (!dir) {
40+
dir = {
41+
models: __dirname + '/../../api/models',
42+
controllers: __dirname + '/../../api/controllers',
43+
services: __dirname + '/../../api/services'
44+
};
45+
}
46+
this.injectAll(dir, next);
47+
},
48+
2849
injectAll: function (dir, cb) {
2950
var self = this;
3051

package.json

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
{
2-
"name" : "sails-util-mvcsloader",
3-
"version": "0.1.7",
2+
"name": "sails-util-mvcsloader",
3+
"version": "0.1.8",
44
"description": "Load the models, controllers, services, policies and config from your Sails hooks into the main app and extend it on your main sails server if you need",
5-
"main" : "index.js",
6-
"scripts" : {
7-
"test": "echo \"Error: no test specified\" && exit 1"
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
88
},
9-
"repository" : {
10-
"type": "git",
11-
"url" : "https://github.com/jaumard/sails-util-mcvsloader.git"
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/jaumard/sails-util-mcvsloader.git"
1212
},
13-
"keywords" : [
14-
"sails",
13+
"keywords": [
14+
"sails",
1515
"sails-hook",
1616
"hook",
17-
"loader",
17+
"loader",
1818
"mvc",
1919
"hook-models",
2020
"hook-controllers",
@@ -27,16 +27,16 @@
2727
"config",
2828
"policies"
2929
],
30-
"author" : "Leeroy Brun, Aumard Jimmy",
31-
"license" : "MIT",
32-
"bugs" : {
33-
"url": "https://github.com/jaumard/sails-util-mvcsloader/issues"
30+
"author": "Leeroy Brun, Aumard Jimmy",
31+
"license": "MIT",
32+
"bugs": {
33+
"url": "https://github.com/jaumard/sails-util-mvcsloader/issues"
3434
},
35-
"homepage" : "https://github.com/jaumard/sails-util-mvcsloader",
35+
"homepage": "https://github.com/jaumard/sails-util-mvcsloader",
3636
"dependencies": {
37-
"async" : "^0.9.0",
38-
"lodash" : "^3.5.0",
39-
"sails-build-dictionary": "^0.10.1",
40-
"sails-util" : "^0.10.6"
37+
"async": "^0.9.0",
38+
"lodash": "^3.5.0",
39+
"sails-build-dictionary": "^0.10.1",
40+
"sails-util": "^0.10.6"
4141
}
4242
}

0 commit comments

Comments
 (0)