Skip to content

Commit

Permalink
Added grails/rails-like dynamic controller/action discovery in the ro…
Browse files Browse the repository at this point in the history
…uting tables for more meaningful controllers.
  • Loading branch information
mikermcneil committed Feb 13, 2012
1 parent 4df55e6 commit 41d6a5f
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 37 deletions.
76 changes: 67 additions & 9 deletions content-server/cmsRouter.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,80 @@
var _ = require('underscore'),
controllers = {
'home' : require('./controllers/indexController'),
'node' : require('./controllers/nodeController'),
'page' : require('./controllers/pageController')
'meta' : require('./controllers/MetaController'),
'node' : require('./controllers/NodeController'),
'page' : require('./controllers/PageController')
};
// model = require('./model');



var mappings = {

'/node': controllers.node.index
, '/node/:id?': controllers.node.view

, '/nodes': controllers.node.index
, '/node/index': controllers.node.index

, '/node/create': controllers.node.create
, '/node/add': controllers.node.create
, '/node/new': controllers.node.create

, '/node/:id?': controllers.node.read
, '/node/read/:id?': controllers.node.read
, '/node/view/:id?': controllers.node.read
, '/node/show/:id?': controllers.node.read
, '/node/detail/:id?': controllers.node.read

, '/node/edit/:id?': controllers.node.update
, '/node/modify/:id?': controllers.node.update
, '/node/update/:id?': controllers.node.update

, '/node/delete/:id?': controllers.node.remove
, '/node/remove/:id?': controllers.node.remove
, '/node/destroy/:id?': controllers.node.remove

, '/sitemap': controllers.page.index
, '/page': controllers.page.index
, '/page/:id?': controllers.page.view
, '/page/view/:id?': controllers.page.view


// Application meta actions
, '/': controllers.meta.home
, '/500': controllers.meta.error
, '/404': controllers.meta.notfound

, '/': controllers.home.index


// Handle wildcard requests
, '/:entity/:action/:id?': function (req,res,next) {
var entity = req.param('entity'),
action = req.param('action');

if (entity &&
entity != "stylesheets" &&
entity != "lib" &&
entity != "sources" &&
entity != "images") {

// Try to map to an entity
if (_.contains(_.keys(controllers),entity)) {
var controller = controllers[entity];
console.log("FOUUND",controller);

// Try to map to a method
if (controller[action]) {
var method = controller[action];
return method(req,res,next);
}
}

// If that fails, just display the 404 page
return res.json({
wildcard: 'fail'
});
//controllers.meta.notfound(req,res,next);
}
else {
next();
}
}
}

// Set up routing table
Expand Down
5 changes: 0 additions & 5 deletions content-server/controllers/indexController.js

This file was deleted.

17 changes: 17 additions & 0 deletions content-server/controllers/metaController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
exports.home = function (req, res, next ) {
res.render('index', {
title: 'crud.io'
});
}

exports.error = function (req, res, next ) {
res.render('index', {
title: 'Error (500) | crud.io'
});
}

exports.notfound = function (req, res, next ) {
res.render('index', {
title: 'Not Found (404) | crud.io'
});
}
37 changes: 20 additions & 17 deletions content-server/controllers/nodeController.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
var _ = require('underscore');

exports.index = function (req, res, next ) {

// // Fetch paginated list of all nodes
// db.Content.gatherByContext(null,function (content) {
// content = _.collect(content,function(val,key) {
// return {
// title: val.title,
// type: val.type,
// payload: val.payload
// }
// });

exports.index = function (req, res, next ) {
res.render('node/index', {
title: 'Manage Content | crud.io',
selected: 'node'
});
// })

}

exports.view = function (req, res, next ){
res.render('node/view', {
title: 'Edit Content Node | crud.io'
exports.read = function (req, res, next ){
console.log(req.params);
res.json({
params: req.params
});
}

exports.remove = function (req, res, next ){
console.log(req.params);
res.json({
params: req.params
});
}


exports.wiggles = function (req, res, next ){
console.log(req.params);
res.json({
params: req.params
});
}
12 changes: 6 additions & 6 deletions content-server/views/layout.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@
</h1>

<ul id="navbar">
<li><a <% if (typeof selected!='undefined' && selected=='page') { %> class='selected' <% } %> href="page">pages</a></li>
<li><a <% if (typeof selected!='undefined' && selected=='node') { %> class='selected' <% } %> href="node">manage content</a></li>
<li><a <% if (typeof selected!='undefined' && selected=='account') { %> class='selected' <% } %> href="account">account</a></li>
<li><a <% if (typeof selected!='undefined' && selected=='page') { %> class='selected' <% } %> href="/page">pages</a></li>
<li><a <% if (typeof selected!='undefined' && selected=='node') { %> class='selected' <% } %> href="/nodes">manage content</a></li>
<li><a <% if (typeof selected!='undefined' && selected=='account') { %> class='selected' <% } %> href="/account">account</a></li>
</ul>

<select class="mobile-only" id="navbar-dropdown">
<option <% if (typeof selected!='undefined' && selected=='page') { %> selected class='selected' <% } %> href="page">pages</option>
<option <% if (typeof selected!='undefined' && selected=='node') { %> selected class='selected' <% } %> href="node">manage content</option>
<option <% if (typeof selected!='undefined' && selected=='account') { %> selected class='selected' <% } %> href="account">account</option>
<option <% if (typeof selected!='undefined' && selected=='page') { %> selected class='selected' <% } %> href="/page">pages</option>
<option <% if (typeof selected!='undefined' && selected=='node') { %> selected class='selected' <% } %> href="/node">manage content</option>
<option <% if (typeof selected!='undefined' && selected=='account') { %> selected class='selected' <% } %> href="/account">account</option>
</select>
</div>

Expand Down

0 comments on commit 41d6a5f

Please sign in to comment.