提交 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,7 +4,7 @@ | ||
| 4 | var mongoose = require('mongoose') | 4 | var mongoose = require('mongoose') |
| 5 | exports = module.exports = home | 5 | exports = module.exports = home |
| 6 | 6 | ||
| 7 | -function home(app, lib, modeluser) { | 7 | +function home(lib, settings) { |
| 8 | return { | 8 | return { |
| 9 | 9 | ||
| 10 | render: function(req, res, next) { | 10 | render: function(req, res, next) { |
| @@ -12,16 +12,10 @@ function home(app, lib, modeluser) { | @@ -12,16 +12,10 @@ function home(app, lib, modeluser) { | ||
| 12 | }, | 12 | }, |
| 13 | 13 | ||
| 14 | about: function(req, res, next) { | 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' ]; | ||
| 21 | +exports['@require'] = [ 'lib', 'settings' ]; |
| @@ -8,30 +8,16 @@ var mongoose = require('mongoose'), | @@ -8,30 +8,16 @@ var mongoose = require('mongoose'), | ||
| 8 | 8 | ||
| 9 | mongooseTypes.loadTypes(mongoose) | 9 | mongooseTypes.loadTypes(mongoose) |
| 10 | 10 | ||
| 11 | -//var Email = mongoose.SchemaTypes.Email | 11 | +var Email = mongoose.SchemaTypes.Email |
| 12 | 12 | ||
| 13 | var User = new Schema({ | 13 | var User = new Schema({ |
| 14 | - /*email: { | 14 | + email: { |
| 15 | type: Email, | 15 | type: Email, |
| 16 | required: true, | 16 | required: true, |
| 17 | unique: true | 17 | unique: true |
| 18 | - }*/ | ||
| 19 | - name: String | 18 | + } |
| 20 | }) | 19 | }) |
| 21 | 20 | ||
| 22 | User.plugin(common) | 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); | ||
| 23 | +module.exports = User; |
| @@ -16,6 +16,7 @@ var errorHandler = require('errorhandler') | @@ -16,6 +16,7 @@ var errorHandler = require('errorhandler') | ||
| 16 | var serveFavicon = require('serve-favicon') | 16 | var serveFavicon = require('serve-favicon') |
| 17 | var cookieParser = require('cookie-parser') | 17 | var cookieParser = require('cookie-parser') |
| 18 | var bodyParser = require('body-parser') | 18 | var bodyParser = require('body-parser') |
| 19 | +var dataNode = require('../lib/dataNode') | ||
| 19 | 20 | ||
| 20 | 21 | ||
| 21 | module.exports = function(lib, callback) { | 22 | module.exports = function(lib, callback) { |
| @@ -35,7 +36,10 @@ module.exports = function(lib, callback) { | @@ -35,7 +36,10 @@ module.exports = function(lib, callback) { | ||
| 35 | ioc.loader('controllers', ioc.node('app/controllers')); | 36 | ioc.loader('controllers', ioc.node('app/controllers')); |
| 36 | ioc.loader('models', ioc.node('app/models')); | 37 | ioc.loader('models', ioc.node('app/models')); |
| 37 | ioc.loader(ioc.node('config')); | 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 | // set the default views directory | 44 | // set the default views directory |
| 41 | app.set('views', lib.config.viewsDir) | 45 | app.set('views', lib.config.viewsDir) |
| @@ -88,10 +92,11 @@ module.exports = function(lib, callback) { | @@ -88,10 +92,11 @@ module.exports = function(lib, callback) { | ||
| 88 | app.use(express.static(lib.config.publicDir, lib.config.staticServer)) | 92 | app.use(express.static(lib.config.publicDir, lib.config.staticServer)) |
| 89 | 93 | ||
| 90 | // error handling | 94 | // error handling |
| 91 | - //app.use(express.errorHandler()) | ||
| 92 | app.use(errorHandler()) | 95 | app.use(errorHandler()) |
| 93 | 96 | ||
| 94 | 97 | ||
| 95 | callback(null, app) | 98 | callback(null, app) |
| 96 | 99 | ||
| 100 | + return {app: app, lib: lib} | ||
| 97 | } | 101 | } |
| 102 | + |
| @@ -2,7 +2,6 @@ | @@ -2,7 +2,6 @@ | ||
| 2 | // # routes | 2 | // # routes |
| 3 | 3 | ||
| 4 | var IoC = require('electrolyte') | 4 | var IoC = require('electrolyte') |
| 5 | - | ||
| 6 | var lib, app | 5 | var lib, app |
| 7 | 6 | ||
| 8 | 7 | ||
| @@ -13,8 +12,7 @@ module.exports = function(_lib, _app) { | @@ -13,8 +12,7 @@ module.exports = function(_lib, _app) { | ||
| 13 | } | 12 | } |
| 14 | 13 | ||
| 15 | function routes() { | 14 | function routes() { |
| 16 | - var self = app; | ||
| 17 | - | 15 | + |
| 18 | var home = IoC.create('controllers/home') | 16 | var home = IoC.create('controllers/home') |
| 19 | 17 | ||
| 20 | app.get('/', home.render) | 18 | app.get('/', home.render) |
| @@ -6,8 +6,6 @@ var config = require('./index'); | @@ -6,8 +6,6 @@ var config = require('./index'); | ||
| 6 | 6 | ||
| 7 | /** | 7 | /** |
| 8 | * Initialize settings. | 8 | * Initialize settings. |
| 9 | - * | ||
| 10 | - * This component configures the application's settings. | ||
| 11 | */ | 9 | */ |
| 12 | exports = module.exports = function(lib, app) { | 10 | exports = module.exports = function(lib, app) { |
| 13 | var settings = new Settings(config); | 11 | var settings = new Settings(config); |
| @@ -21,6 +19,9 @@ exports = module.exports = function(lib, app) { | @@ -21,6 +19,9 @@ exports = module.exports = function(lib, app) { | ||
| 21 | exports['@singleton'] = true; | 19 | exports['@singleton'] = true; |
| 22 | 20 | ||
| 23 | 21 | ||
| 22 | +/** | ||
| 23 | + * Settings component | ||
| 24 | + */ | ||
| 24 | 25 | ||
| 25 | function Settings(initial) { | 26 | function Settings(initial) { |
| 26 | if(initial) | 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' ]; |
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,13 +10,7 @@ module.exports = function(_config) { | ||
| 10 | return mongo | 10 | return mongo |
| 11 | } | 11 | } |
| 12 | 12 | ||
| 13 | -/** | ||
| 14 | - * Component annotations. | ||
| 15 | - */ | ||
| 16 | -exports['@singleton'] = true; | ||
| 17 | - | ||
| 18 | function mongo(callback) { | 13 | function mongo(callback) { |
| 19 | - console.log(config.db); | ||
| 20 | var connection = mongoose.createConnection( | 14 | var connection = mongoose.createConnection( |
| 21 | config.db.host, | 15 | config.db.host, |
| 22 | config.db.dbname, | 16 | config.db.dbname, |
| @@ -12,11 +12,6 @@ module.exports = function(_config) { | @@ -12,11 +12,6 @@ module.exports = function(_config) { | ||
| 12 | return redis | 12 | return redis |
| 13 | } | 13 | } |
| 14 | 14 | ||
| 15 | -/** | ||
| 16 | - * Component annotations. | ||
| 17 | - */ | ||
| 18 | -exports['@singleton'] = true; | ||
| 19 | - | ||
| 20 | function redis(callback) { | 15 | function redis(callback) { |
| 21 | var connection = new RedisStore(config.redis) | 16 | var connection = new RedisStore(config.redis) |
| 22 | connection.on('error', callback) | 17 | connection.on('error', callback) |
| @@ -10,7 +10,6 @@ module.exports = schemas | @@ -10,7 +10,6 @@ module.exports = schemas | ||
| 10 | function schemas(lib, callback) { | 10 | function schemas(lib, callback) { |
| 11 | 11 | ||
| 12 | _.each(models, function(schema, name) { | 12 | _.each(models, function(schema, name) { |
| 13 | - console.log(name, '333'); | ||
| 14 | lib.db.model(name, schema) | 13 | lib.db.model(name, schema) |
| 15 | }) | 14 | }) |
| 16 | 15 |
请
注册
或
登录
后发表评论