-
Notifications
You must be signed in to change notification settings - Fork 0
/
mvc.js
128 lines (109 loc) · 2.97 KB
/
mvc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* Module dependencies.
*/
var fs = require('fs')
, express = require('express');
exports.boot = function(app){
bootApplication(app);
bootControllers(app);
};
// App settings and middleware
function bootApplication(app) {
// Some dynamic view helpers
app.dynamicHelpers({
request: function(req){
return req;
},
hasMessages: function(req){
return Object.keys(req.session.flash || {}).length;
},
//messages: function(req){
// return function(){
// var msgs = req.flash();
// return Object.keys(msgs).reduce(function(arr, type){
// return arr.concat(msgs[type]);
// }, []);
// }
messages: require('express-messages')
});
}
// Bootstrap controllers
function bootControllers(app) {
fs.readdir(__dirname + '/app/controllers', function(err, files){
if (err) throw err;
files.forEach(function(file){
if(file.indexOf(".") != 0) {
bootController(app, file);
}
});
});
}
// Example (simplistic) controller support
function bootController(app, file) {
var name = file.replace('.js', '')
, actions = require('./app/controllers/' + name)
, plural = name + 's' // realistically we would use an inflection lib
, prefix = '/' + plural;
// Special case for "app"
//if (name == 'app') prefix = '/wcps';
Object.keys(actions).map(function(action){
var fn = controllerAction(name, plural, action, actions[action]);
switch(action) {
case 'index':
app.get(prefix, fn);
break;
case 'show':
app.get(prefix + '/:id.:format?', fn);
break;
case 'add':
app.get(prefix + '/:id/add', fn);
break;
case 'create':
app.post(prefix + '/:id', fn);
break;
case 'edit':
app.get(prefix + '/:id/edit', fn);
break;
case 'update':
app.put(prefix + '/:id', fn);
break;
case 'destroy':
app.del(prefix + '/:id', fn);
break;
}
});
}
// Proxy res.render() to add some magic
function controllerAction(name, plural, action, fn) {
return function(req, res, next){
var render = res.render
, format = req.params.format
, path = __dirname + '/app/views/' + name + '/' + action + '.html';
res.render = function(obj, options, fn){
res.render = render;
// Template path
if (typeof obj === 'string') {
return res.render(obj, options, fn);
}
// Format support
if (action == 'show' && format) {
if (format === 'json') {
return res.send(obj);
} else {
throw new Error('unsupported format "' + format + '"');
}
}
// Render template
res.render = render;
options = options || {};
// Expose obj as the "users" or "user" local
if (action == 'index') {
options[plural] = obj;
} else {
options[name] = obj;
}
return res.render(path, options, fn);
};
fn.apply(this, arguments);
};
}