-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (35 loc) · 1.17 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
var buildDictionary = require('sails-build-dictionary');
var path = require('path');
module.exports = function (sails) {
return {
initialize: function (next) {
loadSerializers(function(err, serializers) {
sails.serializers = { all_serializers: serializers };
// Expose services as globals
_.each(sails.services, function(service) {
var globalName = service.globalId || service.identity;
global[globalName] = service;
});
});
next();
}
}
function loadSerializers(cb) {
dic_options = {
dirname : path.resolve(sails.config.appPath, 'api/serializers'),
filter : /(.+)(Serializer)\.(js|coffee|litcoffee)$/,
depth :5,
useGlobalIdForKeyName: true,
identity: false
}
buildDictionary.optional(dic_options, bindToSails(cb))
};
function bindToSails(cb) {
return function(err, serializers) {
if(err) { return cb(err); }
serializers = _.map(serializers, function(v, k) { var ser = {}; ser[k] = v; return ser })
_.forEach(serializers, function(serializer) { _.bindAll(serializer); });
return cb(null, serializers);
}
};
};