提交 b874693d462be00007cdae50b41d385de6df9bd7
1 个父辈
0dc931d4
Implemented data injection feature to support asynchronously generated objects l…
…ike lib, some code clean-up, added mongoose example
正在显示
12 个修改的文件
包含
79 行增加
和
51 行删除
... | ... | @@ -4,7 +4,7 @@ |
4 | 4 | var mongoose = require('mongoose') |
5 | 5 | exports = module.exports = home |
6 | 6 | |
7 | -function home(app, lib, modeluser) { | |
7 | +function home(lib, settings) { | |
8 | 8 | return { |
9 | 9 | |
10 | 10 | render: function(req, res, next) { |
... | ... | @@ -12,16 +12,10 @@ function home(app, lib, modeluser) { |
12 | 12 | }, |
13 | 13 | |
14 | 14 | about: function(req, res, next) { |
15 | - // save project data | |
16 | - var User = mongoose.model('User', modeluser); | |
17 | - console.log(modeluser); | |
18 | - User.create({name: 'john'}, function( err, project ) { | |
19 | - res.send(200, 'Hello, this is Igloo.') | |
20 | - }); | |
21 | - | |
15 | + res.send(200, 'Hello, this is Igloo.') | |
22 | 16 | } |
23 | 17 | |
24 | 18 | } |
25 | 19 | } |
26 | 20 | |
27 | -exports['@require'] = [ 'mongo', 'redis', 'models/user' ]; | |
\ No newline at end of file | ||
21 | +exports['@require'] = [ 'lib', 'settings' ]; | |
\ No newline at end of file | ... | ... |
... | ... | @@ -8,30 +8,16 @@ var mongoose = require('mongoose'), |
8 | 8 | |
9 | 9 | mongooseTypes.loadTypes(mongoose) |
10 | 10 | |
11 | -//var Email = mongoose.SchemaTypes.Email | |
11 | +var Email = mongoose.SchemaTypes.Email | |
12 | 12 | |
13 | 13 | var User = new Schema({ |
14 | - /*email: { | |
14 | + email: { | |
15 | 15 | type: Email, |
16 | 16 | required: true, |
17 | 17 | unique: true |
18 | - }*/ | |
19 | - name: String | |
18 | + } | |
20 | 19 | }) |
21 | 20 | |
22 | 21 | User.plugin(common) |
23 | 22 | |
24 | -module.exports = User; | |
25 | - | |
26 | - | |
27 | -var MUser = mongoose.model('User', User); | |
28 | - | |
29 | -var user = new MUser({ | |
30 | - name: 'user@example.com', | |
31 | -}); | |
32 | - | |
33 | -MUser.create({name: 'john'}, function( err, project ) { | |
34 | - console.log(200, 'Hello, this is Igloo.') | |
35 | -}); | |
36 | - | |
37 | -console.log(MUser.db); | |
\ No newline at end of file | ||
23 | +module.exports = User; | |
\ No newline at end of file | ... | ... |
... | ... | @@ -16,6 +16,7 @@ var errorHandler = require('errorhandler') |
16 | 16 | var serveFavicon = require('serve-favicon') |
17 | 17 | var cookieParser = require('cookie-parser') |
18 | 18 | var bodyParser = require('body-parser') |
19 | +var dataNode = require('../lib/dataNode') | |
19 | 20 | |
20 | 21 | |
21 | 22 | module.exports = function(lib, callback) { |
... | ... | @@ -35,7 +36,10 @@ module.exports = function(lib, callback) { |
35 | 36 | ioc.loader('controllers', ioc.node('app/controllers')); |
36 | 37 | ioc.loader('models', ioc.node('app/models')); |
37 | 38 | ioc.loader(ioc.node('config')); |
38 | - ioc.loader(ioc.node('lib')); | |
39 | + | |
40 | + // set-up data nodes (inject data) | |
41 | + ioc.loader(dataNode(lib, 'lib')); | |
42 | + ioc.loader(dataNode(app, 'app')); | |
39 | 43 | |
40 | 44 | // set the default views directory |
41 | 45 | app.set('views', lib.config.viewsDir) |
... | ... | @@ -88,10 +92,11 @@ module.exports = function(lib, callback) { |
88 | 92 | app.use(express.static(lib.config.publicDir, lib.config.staticServer)) |
89 | 93 | |
90 | 94 | // error handling |
91 | - //app.use(express.errorHandler()) | |
92 | 95 | app.use(errorHandler()) |
93 | 96 | |
94 | 97 | |
95 | 98 | callback(null, app) |
96 | 99 | |
100 | + return {app: app, lib: lib} | |
97 | 101 | } |
102 | + | ... | ... |
... | ... | @@ -2,7 +2,6 @@ |
2 | 2 | // # routes |
3 | 3 | |
4 | 4 | var IoC = require('electrolyte') |
5 | - | |
6 | 5 | var lib, app |
7 | 6 | |
8 | 7 | |
... | ... | @@ -13,8 +12,7 @@ module.exports = function(_lib, _app) { |
13 | 12 | } |
14 | 13 | |
15 | 14 | function routes() { |
16 | - var self = app; | |
17 | - | |
15 | + | |
18 | 16 | var home = IoC.create('controllers/home') |
19 | 17 | |
20 | 18 | app.get('/', home.render) | ... | ... |
... | ... | @@ -6,8 +6,6 @@ var config = require('./index'); |
6 | 6 | |
7 | 7 | /** |
8 | 8 | * Initialize settings. |
9 | - * | |
10 | - * This component configures the application's settings. | |
11 | 9 | */ |
12 | 10 | exports = module.exports = function(lib, app) { |
13 | 11 | var settings = new Settings(config); |
... | ... | @@ -21,6 +19,9 @@ exports = module.exports = function(lib, app) { |
21 | 19 | exports['@singleton'] = true; |
22 | 20 | |
23 | 21 | |
22 | +/** | |
23 | + * Settings component | |
24 | + */ | |
24 | 25 | |
25 | 26 | function Settings(initial) { |
26 | 27 | if(initial) | ... | ... |
examples/mongoose/controller.create.js
0 → 100644
1 | +/** | |
2 | + * Mongoose create route. | |
3 | + * Use this in a controller and setup a route | |
4 | + * to create function. | |
5 | + */ | |
6 | + | |
7 | +exports = module.exports = home | |
8 | + | |
9 | +function home(db, _User) { | |
10 | + return { | |
11 | + | |
12 | + create: function(req, res, next) { | |
13 | + | |
14 | + // create user object using db ('lib/db') connection | |
15 | + // and models/user | |
16 | + | |
17 | + var User = db.model('User', _User); | |
18 | + | |
19 | + User.create({email: 'somebody@example.com'}, function( err, user ) { | |
20 | + if(err) | |
21 | + res.send(500, 'Igloo: Something went wrong.') | |
22 | + else | |
23 | + res.send(200, 'Igloo: Created user.') | |
24 | + }); | |
25 | + | |
26 | + } | |
27 | + | |
28 | + } | |
29 | +} | |
30 | + | |
31 | +exports['@require'] = [ 'lib/db', 'models/user' ]; | |
\ No newline at end of file | ... | ... |
lib/dataNode.js
0 → 100644
1 | + | |
2 | +/** | |
3 | + * This is the missing data injection functionality in | |
4 | + * electrolyte. Based on electrolyte/loaders/node.js, the | |
5 | + * default script loader. | |
6 | + * | |
7 | + */ | |
8 | + | |
9 | +module.exports = function(data, options) { | |
10 | + if ('string' == typeof options) { | |
11 | + options = { dirname: options } | |
12 | + } | |
13 | + | |
14 | + return function(id) { | |
15 | + | |
16 | + var newId = null; | |
17 | + | |
18 | + if(id){ | |
19 | + var dir = options.dirname + '/' | |
20 | + | |
21 | + if(id.indexOf(dir) === 0) | |
22 | + newId = id.substr(dir.length) | |
23 | + } | |
24 | + | |
25 | + if(newId) | |
26 | + return data[newId] | |
27 | + else | |
28 | + return data | |
29 | + } | |
30 | +} | ... | ... |
... | ... | @@ -10,13 +10,7 @@ module.exports = function(_config) { |
10 | 10 | return mongo |
11 | 11 | } |
12 | 12 | |
13 | -/** | |
14 | - * Component annotations. | |
15 | - */ | |
16 | -exports['@singleton'] = true; | |
17 | - | |
18 | 13 | function mongo(callback) { |
19 | - console.log(config.db); | |
20 | 14 | var connection = mongoose.createConnection( |
21 | 15 | config.db.host, |
22 | 16 | config.db.dbname, | ... | ... |
... | ... | @@ -12,11 +12,6 @@ module.exports = function(_config) { |
12 | 12 | return redis |
13 | 13 | } |
14 | 14 | |
15 | -/** | |
16 | - * Component annotations. | |
17 | - */ | |
18 | -exports['@singleton'] = true; | |
19 | - | |
20 | 15 | function redis(callback) { |
21 | 16 | var connection = new RedisStore(config.redis) |
22 | 17 | connection.on('error', callback) | ... | ... |
请
注册
或
登录
后发表评论