Permalink
Browse files

Modified the way to add singleton resources. Singleton resources can …

…now have nested resources.
  • Loading branch information...
1 parent c8353c2 commit 20a03ce2886903519af522410304c45a5b088526 @olalonde olalonde committed Oct 5, 2012
Showing with 67 additions and 23 deletions.
  1. +18 −17 README.md
  2. +29 −0 examples/singleton/server.js
  3. +17 −5 lib/railway_routes.js
  4. +3 −1 package.json
View
35 README.md
@@ -60,23 +60,24 @@ 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:
+
+ map.resource('account');
+
+Will generate the following routes:
+
+ GET /account account#show
+ POST /account account#create
+ GET /account/new account#new
+ GET /account/edit account#edit
+ DELETE /account account#destroy
+ PUT /account account#update
+
+Singleton resources can also have nested resources. For example:
+
+ map.resource('account', function(account) {
+ map.resources('posts');
+ });
## Example app
View
29 examples/singleton/server.js
@@ -0,0 +1,29 @@
+var express = require('express')
+ , Map = require('../../lib/railway_routes').Map;
+
+var app = express();
+
+app.use(app.router);
+
+var map = new Map(app, function(namespace, controller, action) {
+ console.log(arguments);
+});
+
+map.resources('users', function(user) {
+ map.resources('posts', function(post) {
+ map.resources('comments');
+ });
+ //user.singleton('account');
+});
+
+map.resource('account', {controller: 'users'}, function(account) {
+ map.resources('posts', function(post) {
+ map.resources('comments');
+ });
+});
+
+console.log(app.routes);
+
+app.listen(3000, function(){
+ console.log('Express server listening at http://localhost:3000');
+});
View
22 lib/railway_routes.js
@@ -228,9 +228,8 @@ Map.prototype.resources = function (name, params, actions) {
var activeRoutes = getActiveRoutes(params);
// but first, create subroutes
if (typeof actions == 'function') {
- // if Resource is a singleton, we don't need to append /:resource_id
- if(params.singleton_for)
- this.subroutes(name + '/', actions);
+ if (params.singleton)
+ this.subroutes(name, actions); // singletons don't need to specify an id
else
this.subroutes(name + '/:' + (singularize(name) || name) + '_id', actions);
}
@@ -264,7 +263,7 @@ 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;
+ var controller = name;
// and call map.{get|post|update|delete}
// with the path, controller, middleware and options
@@ -299,7 +298,7 @@ Map.prototype.resources = function (name, params, actions) {
, 'update': 'PUT /'
};
- if(params.singleton_for)
+ if (params.singleton)
availableRoutes = availableRoutesSingleton;
// 1. only
@@ -352,6 +351,19 @@ Map.prototype.resources = function (name, params, actions) {
}
};
+Map.prototype.resource = function(name, params, actions) {
+ var self = this;
+ // params are optional
+ params = params || {};
+ // if params arg omitted, second arg may be `actions`
+ if (typeof params == 'function') {
+ actions = params;
+ params = {};
+ }
+ params.singleton = true;
+ return this.resources(name, params, actions);
+}
+
/*
* Namespaces mapper.
*
View
4 package.json
@@ -16,7 +16,9 @@
"engines": {
"node": ">= 0.4.0"
},
- "dependencies": {},
+ "dependencies": {
+ "express": "3.x"
+ },
"devDependencies": {
"nodeunit": "*"
}

0 comments on commit 20a03ce

Please sign in to comment.