Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #8 from olalonde/master

Modified the way to add singleton resources.
  • Loading branch information...
commit 712b2bcc06ef69997a1a176725182a117487ce8d 2 parents 384f578 + f1f5ad2
@1602 authored
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) {
+ account.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
35 lib/railway_routes.js
@@ -224,11 +224,27 @@ Map.prototype.resources = function (name, params, actions) {
actions = params;
params = {};
}
+
+ // If resource uses the path param, it's subroutes should be
+ // prefixed by path, not the resource's name
+ // i.e.:
+ // map.resource('users', {path: ':username'}, function(user) {
+ // user.resources('posts);
+ // });
+ //
+ // /:username/posts.:format?
+ // /:username/posts/new.:format?
+ // etc.
+ var prefix = params.path ? params.path : name;
+
// we have bunch of actions here, will create routes for them
var activeRoutes = getActiveRoutes(params);
// but first, create subroutes
if (typeof actions == 'function') {
- this.subroutes(name + '/:' + (singularize(name) || name) + '_id', actions);
+ if (params.singleton)
+ this.subroutes(prefix, actions); // singletons don't need to specify an id
+ else
+ this.subroutes(prefix + '/:' + (singularize(name) || name) + '_id', actions);
}
// now let's walk through action routes
for (var action in activeRoutes) {
@@ -260,7 +276,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 = params.controller || name;
// and call map.{get|post|update|delete}
// with the path, controller, middleware and options
@@ -295,7 +311,7 @@ Map.prototype.resources = function (name, params, actions) {
, 'update': 'PUT /'
};
- if(params.singleton_for)
+ if (params.singleton)
availableRoutes = availableRoutesSingleton;
// 1. only
@@ -348,6 +364,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": "*"
}
Please sign in to comment.
Something went wrong with that request. Please try again.