-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathActions.js
79 lines (74 loc) · 2.11 KB
/
Actions.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
var Backbone = require("backbone");
var ActionResponse = require("./ActionResponse");
var _ = require("underscore");
module.exports = function(attributes) {
for(var key in attributes)
this[key] = attributes[key];
this.request = this.request || function(req) {};
this.response = this.response || ActionResponse.augment;
}
module.exports.prototype = {
registerAction : function(app, method, url, action) {
var request = this.request;
var response = this.response;
var handler = function(req, res, next){
request(req);
response(res);
action(req, res, next);
};
var args = [url];
if(Array.isArray(action)) {
_.each(action, function(a){
args.push(function(req, res, next){
request(req);
response(res);
a(req, res, next);
});
});
} else {
args.push(function(req, res, next){
request(req);
response(res);
action(req, res, next);
});
}
switch(method) {
case "GET":
app.get.apply(app, args);
break;
case "POST":
app.post.apply(app, args);
break;
case "PUT":
app.put.apply(app, args);
break;
case "DELETE":
app.del.apply(app, args);
break;
}
},
exportHttpActions : function(app, root, actions) {
var root = actions.root || root;
for(var key in actions) {
if(key == "routes") {
this.exportHttpActions(app, root, actions.routes);
continue;
}
if(key.indexOf(" ") === -1) continue;
var parts = key.split(" ");
var method = parts.shift();
var url = parts.pop();
var actionHandler = actions[key];
if(typeof actionHandler === "string") {
actionHandler = this[actionHandler];
if(typeof actionHandler !== "function" && !Array.isArray(actionHandler))
throw new Error(actionHandler+" was not found");
}
this.registerAction(app, method, root+url, actionHandler);
}
},
registerActions: function(app, root){
this.exportHttpActions(app, root || "", this);
}
}
module.exports.extend = Backbone.View.extend;