express-resource
Advanced tools
Comparing version 0.2.0 to 0.2.1
0.2.1 / 2011-05-25 | ||
================== | ||
* Fixed for express 2.3.9. Closes #18 | ||
* Added better support for mapping your own actions. Closes #7 | ||
0.2.0 / 2011-04-09 | ||
@@ -3,0 +9,0 @@ ================== |
35
index.js
@@ -14,2 +14,3 @@ | ||
var express = require('express') | ||
, join = require('path').join | ||
, lingo = require('lingo') | ||
@@ -28,3 +29,2 @@ , en = lingo.en; | ||
var Resource = module.exports = function Resource(name, actions, app) { | ||
this.base = '/'; | ||
this.name = name; | ||
@@ -34,2 +34,4 @@ this.app = app; | ||
actions = actions || {}; | ||
this.base = actions.base || '/'; | ||
if ('/' != this.base[this.base.length - 1]) this.base += '/'; | ||
this.format = actions.format; | ||
@@ -99,3 +101,4 @@ this.id = actions.id || this.defaultId; | ||
Resource.prototype.map = function(method, path, fn){ | ||
var self = this; | ||
var self = this | ||
, orig = path; | ||
@@ -106,3 +109,3 @@ if (method instanceof Resource) return this.add(method); | ||
if ('/' == path[0]) path = path.substr(1); | ||
else path = join(this.param, path); | ||
method = method.toLowerCase(); | ||
@@ -112,3 +115,3 @@ | ||
var route = this.base + (this.name || ''); | ||
route += (this.name && path) ? '/' : ''; | ||
if (this.name && path) route += '/'; | ||
route += path; | ||
@@ -121,3 +124,3 @@ route += '.:format?'; | ||
, path: route | ||
, orig: path | ||
, orig: orig | ||
, fn: fn | ||
@@ -136,3 +139,3 @@ }; | ||
} else { | ||
res.send(415); | ||
res.send(406); | ||
} | ||
@@ -157,3 +160,3 @@ } else { | ||
Resource.prototype.add = function(resource){ | ||
var router = this.app.router | ||
var app = this.app | ||
, routes | ||
@@ -171,3 +174,3 @@ , route; | ||
delete routes[key]; | ||
router.remove(key, route.method); | ||
app[method](key).remove(); | ||
resource.map(route.method, route.orig, route.fn); | ||
@@ -189,25 +192,23 @@ } | ||
Resource.prototype.mapDefaultAction = function(key, fn){ | ||
var id = this.param; | ||
switch (key) { | ||
case 'index': | ||
this.get(fn); | ||
this.get('/', fn); | ||
break; | ||
case 'new': | ||
this.get('new', fn); | ||
this.get('/new', fn); | ||
break; | ||
case 'create': | ||
this.post(fn); | ||
this.post('/', fn); | ||
break; | ||
case 'show': | ||
this.get(id, fn); | ||
this.get(fn); | ||
break; | ||
case 'edit': | ||
this.get(id + '/edit', fn); | ||
this.get('edit', fn); | ||
break; | ||
case 'update': | ||
this.put(id, fn); | ||
this.put(fn); | ||
break; | ||
case 'destroy': | ||
this.del(id, fn); | ||
this.del(fn); | ||
break; | ||
@@ -214,0 +215,0 @@ } |
{ "name": "express-resource" | ||
, "description": "Resourceful routing for express" | ||
, "version": "0.2.0" | ||
, "version": "0.2.1" | ||
, "author": "TJ Holowaychuk <tj@vision-media.ca>" | ||
@@ -9,2 +9,11 @@ , "contributors": [ | ||
, "dependencies": { "lingo": ">= 0.0.4" } | ||
, "devDependencies": { | ||
"connect": "1.4.x" | ||
, "express": "2.3.x" | ||
, "ejs": "0.4.x" | ||
, "expresso": "0.7.x" | ||
, "lingo": "0.0.4" | ||
, "qs": "0.1.x" | ||
, "should": ">= 0.0.1" | ||
} | ||
, "keywords": ["express", "rest", "resource"] | ||
@@ -11,0 +20,0 @@ , "main": "index" |
@@ -1,2 +0,1 @@ | ||
# Express Resource | ||
@@ -29,15 +28,15 @@ | ||
exports.show = function(req, res){ | ||
res.send('show forum ' + req.params.id); | ||
res.send('show forum ' + req.params.forum); | ||
}; | ||
exports.edit = function(req, res){ | ||
res.send('edit forum ' + req.params.id); | ||
res.send('edit forum ' + req.params.forum); | ||
}; | ||
exports.update = function(req, res){ | ||
res.send('update forum ' + req.params.id); | ||
res.send('update forum ' + req.params.forum); | ||
}; | ||
exports.destroy = function(req, res){ | ||
res.send('destroy forum ' + req.params.id); | ||
res.send('destroy forum ' + req.params.forum); | ||
}; | ||
@@ -127,3 +126,3 @@ | ||
default: | ||
res.send(415); | ||
res.send(406); | ||
} | ||
@@ -150,3 +149,3 @@ }; | ||
exports.default = function(req, res){ | ||
res.send('Unsupported format "' + req.format + '"', 415); | ||
res.send('Unsupported format "' + req.format + '"', 406); | ||
}; | ||
@@ -153,0 +152,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
17667
14
351
7
189