-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
49 lines (37 loc) · 1.48 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
var _ = require('lodash');
var BasePlugin = require('mixdown-app').Plugin;
var JsonPlugin = BasePlugin.extend({
init: function(options) {
// ensure boolean.
options.jsonpEnabled = options.jsonpEnabled === true || options.jsonpEnabled === 'true' ? true : false;
this._super(options);
},
/**
* Takes the given model and generated the main object that will be passed to the rendering engine.
* This will include the utils, underscore, siteConfig, and other basic rendering constructs.
**/
/**
* Takes the given model and generated the main object that will be passed to the rendering engine.
* This will include the utils, underscore, siteConfig, and other basic rendering constructs.
* @param res {HttpServerResponse} the res to write the output.
* @param data {Object} Data to pass to the rendering view
* @param data {Object} Keys/values for headers (This action will set the Content_Type: 'application/json' automatically)
**/
send: function(data, res, headers, req) {
var str = JSON.stringify(data || {});
if (!res.headersSent) {
var h = _.extend({
'Content-Type': 'application/json'
}, this._options.headers || {});
res.writeHead(200, _.extend(h, headers));
}
if (this._options.jsonpEnabled && req && req.query) {
var cb = req.query.callback ? req.query.callback : req.query.jsonp;
if (cb) {
str = cb + '(' + str + ')';
}
}
res.end(str);
}
});
module.exports = JsonPlugin;