-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
164 lines (142 loc) · 5.52 KB
/
index.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
var jade = require('jade');
var jadeAsync = require('jade-async');
var _ = require('lodash');
var path = require('path');
var util = require('util');
var jadeHelpers = require('./jade-helpers.js');
module.exports = function (sails) {
var jadeLocals = {};
var extendJadeLocals = function (locals){
return _.assign(jadeLocals, locals);
};
return {
initialize: function(cb) {
console.log('sails-hook-cms:initialize');
jadeLocals.sails = sails;
jadeLocals.helpers = jadeHelpers(sails);
jadeLocals._ = _;
//This adds the validation function cms as [model].type = cms = function(){}
//This is to prevent
for (var key in sails.models) {
if (sails.models.hasOwnProperty(key)) {
if(!sails.models[key].types) sails.models[key].types = {};
sails.models[key].types.cms= function(){
return true;
};
}
}
return cb();
},
routes: {
after: {
'GET /admin': function (req, res, next) {
var jadeFn = jade.compileFile(path.join(__dirname, 'views/home.jade'));
var html = jadeFn(extendJadeLocals({}));
return res.send(html);
},
'GET /admin/:model': function (req, res, next) {
if(req.params.model && sails.models[req.params.model]){
model = sails.models[req.params.model];
var modelSchema = model._attributes;
//Find all models
model.find().populateAll().exec(function(err, models){
if(err) return res.negotiate(err);
var jadeFn = jade.compileFile(path.join(__dirname, 'views/model.index.jade'));
var html = jadeFn(extendJadeLocals({
modelName: req.params.model,
modelSchema: modelSchema,
cms: model.cms || {},
models: models,
primaryKey: model.primaryKey
}));
return res.ok(html);
});
} else {
return next();
}
},
'GET /admin/:model/create': function(req, res, next){
if(req.params.model && sails.models[req.params.model]){
var modelSchema = _.clone(sails.models[req.params.model]._attributes);
delete modelSchema.id;
delete modelSchema.createdAt;
delete modelSchema.updatedAt;
//Using the async thing
var jadeFn = jadeAsync.compileFile(path.join(__dirname, 'views/model.create.jade'));
jadeFn(extendJadeLocals({
modelName: req.params.model,
modelSchema: modelSchema
})).done(function (html) {
return res.send(html);
});
} else {
return next();
}
},
'GET /admin/:model/edit/:modelId': function(req, res, next){
if(req.params.modelId && req.params.model && sails.models[req.params.model]){
var modelSchema = _.clone(sails.models[req.params.model]._attributes);
delete modelSchema.id;
delete modelSchema.createdAt;
delete modelSchema.updatedAt;
//FindOne model
model = sails.models[req.params.model]
model
.findOne(req.params.modelId)
.exec(function(err, item){
if(err || (item == undefined)) return res.negotiate(err);
var jadeFn = jadeAsync.compileFile(path.join(__dirname, 'views/model.edit.jade'));
jadeFn(extendJadeLocals({
modelName: req.params.model,
modelSchema: modelSchema,
item:item,
primaryKey:model.primaryKey
})).done(function (html) {
return res.send(html);
});
});
} else {
return next();
}
},
'POST /admin/:model/store': function(req, res, next){
console.log(req.body);
if(req.params.model && sails.models[req.params.model]){
req.body = _.pick(req.body, _.identity); //Cleans req.body from empty attrs or _.omit(sourceObj, _.isUndefined) <- allows false, null, 0
sails.models[req.params.model].create(req.body).exec(function(err, model){
if(err) return res.negotiate(err);
return res.redirect('/admin/' + req.params.model);
});
} else {
return next();
}
},
'POST /admin/:model/update/:modelId': function(req, res, next){
console.log(req.body);
if(req.params.model && sails.models[req.params.model]){
req.body = _.pick(req.body, _.identity); //Cleans req.body from empty attrs or _.omit(sourceObj, _.isUndefined) <- allows false, null, 0
sails.models[req.params.model].update(req.params.modelId, req.body).exec(function(err, model){
if(err) return res.negotiate(err);
return res.redirect('/admin/' + req.params.model);
});
} else {
return next();
}
},
'GET /admin/:model/delete/:modelId': function(req, res, next){
if(req.params.modelId && req.params.model && sails.models[req.params.model]){
//FindOne model
sails.models[req.params.model]
.destroy(req.params.modelId)
.exec(function(err, model){
if(err) return res.negotiate(err);
return res.redirect('/admin/' + req.params.model);
});
} else {
return next();
}
}
}
}
};
};