Skip to content
Browse files

emcee@0.0.3

  • Loading branch information...
1 parent 6ced7b6 commit e63462d39f8fc871d2efcf2b19f967d6dddd6ad9 @isaacs isaacs committed
Showing with 38 additions and 23 deletions.
  1. +3 −2 node_modules/emcee/README.md
  2. +21 −6 node_modules/emcee/emcee.js
  3. +3 −15 node_modules/emcee/package.json
  4. +11 −0 node_modules/emcee/test/basic.js
View
5 node_modules/emcee/README.md
@@ -27,8 +27,11 @@ http.createServer(function (req, res) {
// check if the user is logged in.
var m = new MC()
+ // modelname, args...
m.load('login', req)
m.load('bike', req.url, res)
+ // different kind of bike: modelname, alias, args...
+ m.loadAs('bike', 'motorbike', 'dirt', 'roads')
m.end(function (er, models) {
// either there is an error, or all models are loaded on the
@@ -39,5 +42,3 @@ http.createServer(function (req, res) {
})
}).listen(1337)
```
-
-
View
27 node_modules/emcee/emcee.js
@@ -19,22 +19,26 @@ MC.model = function (name, loader) {
return MC
}
-MC.prototype.load = function (name) {
+MC.prototype.loadAs = function (name, alias) {
if (!modelLoaders[name]) {
throw new Error('Unknown model: ' + name)
}
if (this.error) return
- var a = new Array(arguments.length)
- for (var i = 1; i < arguments.length; i ++) {
- a[i-1] = arguments[i]
+ if (!alias) alias = name
+
+ // [name, alias, args...] => [args..., cb]
+ var a = new Array(arguments.length - 1)
+ for (var i = 2; i < arguments.length; i ++) {
+ a[i - 2] = arguments[i]
}
- a[i-1] = next.bind(this)
+ a[i - 2] = next.bind(this)
+
this.loading ++
modelLoaders[name].apply(this, a)
function next (er, data) {
if (this.error) return
this.error = er
- this[name] = this.models[name] = data
+ this[alias] = this.models[alias] = data
this.loading --
if ((er || this.loading === 0) && this.ondone) {
@@ -43,6 +47,17 @@ MC.prototype.load = function (name) {
}
}
+MC.prototype.load = function (name) {
+ var a = new Array(arguments.length + 1)
+ a[0] = name
+ for (var i = 0; i < arguments.length; i ++) {
+ a[i+1] = arguments[i]
+ }
+ // a = [name, name, args...]
+
+ return this.loadAs.apply(this, a)
+}
+
MC.prototype.end = function (cb) {
this.ondone = cb
if (this.loading === 0 || this.error) {
View
18 node_modules/emcee/package.json
@@ -6,7 +6,7 @@
},
"name": "emcee",
"description": "A bridge between the M and C bits of MVC",
- "version": "0.0.2",
+ "version": "0.0.3",
"repository": {
"type": "git",
"url": "git://github.com/isaacs/emcee.git"
@@ -18,19 +18,7 @@
"devDependencies": {
"tap": "~0.2.5"
},
- "_npmUser": {
- "name": "isaacs",
- "email": "i@izs.me"
- },
- "_id": "emcee@0.0.2",
- "dependencies": {},
- "optionalDependencies": {},
- "engines": {
- "node": "*"
- },
- "_engineSupported": true,
- "_npmVersion": "1.1.24",
- "_nodeVersion": "v0.7.10-pre",
- "_defaultsLoaded": true,
+ "readme": "# emcee\n\nA model loader that a controller calls. Basically, just the M and C\nbits of MVC.\n\nAn interface for controllers to specify that certain models will be\nneeded, and then have them get loaded.\n\n## Usage\n\n```javascript\nvar MC = require('emcee')\n\n// add a bunch of models\nMC.model('login', function (req, cb) {\n // callback is called with (er, data)\n req.session.get('login', cb)\n})\n\nMC.model('train', function (url, res, cb) {\n \n})\n\n// later on...\n\nhttp.createServer(function (req, res) {\n // check if the user is logged in.\n var m = new MC()\n\n // modelname, args...\n m.load('login', req)\n m.load('bike', req.url, res)\n // different kind of bike: modelname, alias, args...\n m.loadAs('bike', 'motorbike', 'dirt', 'roads')\n\n m.end(function (er, models) {\n // either there is an error, or all models are loaded on the\n // 'models' object. note that all errors are assumed to be\n // catastrophic, so you only get the first error, and the\n // models object will only contain the models that got loaded\n // before the error occurred.\n })\n}).listen(1337)\n```\n",
+ "_id": "emcee@0.0.3",
"_from": "emcee@0"
}
View
11 node_modules/emcee/test/basic.js
@@ -50,3 +50,14 @@ tap.test('multiple', function (t) {
t.end()
})
})
+
+tap.test('aliasing', function (t) {
+ var m = new MC()
+ m.load('file', __filename)
+ m.loadAs('file', 'otherfile', __filename)
+ m.end(function (er, models) {
+ if (er) throw er
+ t.same(models.file, models.otherfile)
+ t.end()
+ })
+})

0 comments on commit e63462d

Please sign in to comment.
Something went wrong with that request. Please try again.