Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #7 from olalonde/master

Added singleton resources functionality
  • Loading branch information...
commit 384f578e90bb57b1488d97526a70e46ab288451f 2 parents 6f05378 + 832f869
@1602 authored
Showing with 34 additions and 1 deletion.
  1. +20 −0 README.md
  2. +14 −1 lib/railway_routes.js
View
20 README.md
@@ -58,6 +58,26 @@ In that case your handler should be:
http://railwayjs.com/routing.html
+### Singleton resources
+
+To create a singleton resource, set the singleton_for property of the
+params hash to the resource you want the singleton resource to map to.
+
+Example usage:
+
+ map.resources('users');
+ map.resources('account', {
+ singleton_for: 'users',
+ middleware: function(req, res, next) {
+ // Set the id parameter since we don't get it from the URL
+ // like is usually the case for normal resources.
+ if(req.user)
+ req.params.id = req.user.id;
+ else
+ return res.redirect('/login');
+ next();
+ }});
+
## Example app
Check out [example app][1] to deal with middleware, route handling, and generic routes:
View
15 lib/railway_routes.js
@@ -260,12 +260,14 @@ Map.prototype.resources = function (name, params, actions) {
// params.path setting allows to override common path component
var effectivePath = (params.path || name) + path;
+ var controller = (params.singleton_for) ? params.singleton_for : name;
+
// and call map.{get|post|update|delete}
// with the path, controller, middleware and options
this[method.toLowerCase()].call(
this,
effectivePath,
- name + '#' + action,
+ controller + '#' + action,
skipMiddleware ? [] : params.middleware,
getParams(action, params)
);
@@ -283,8 +285,19 @@ Map.prototype.resources = function (name, params, actions) {
, 'destroy': 'DELETE /:id'
, 'update': 'PUT /:id'
, 'show': 'GET /:id'
+ },
+ availableRoutesSingleton =
+ { 'show': 'GET /'
+ , 'create': 'POST /'
+ , 'new': 'GET /new'
+ , 'edit': 'GET /edit'
+ , 'destroy': 'DELETE /'
+ , 'update': 'PUT /'
};
+ if(params.singleton_for)
+ availableRoutes = availableRoutesSingleton;
+
// 1. only
if (params.only) {
if (typeof params.only == 'string') {
Please sign in to comment.
Something went wrong with that request. Please try again.