Last active
August 29, 2015 14:03
-
-
Save JemiloII/afe41cc9801d7ae78766 to your computer and use it in GitHub Desktop.
Changing up my json to fit ember
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 200 (OK) Response | |
* | |
* Usage: | |
* return res.ok(); | |
* return res.ok(data); | |
* return res.ok(data, view); | |
* return res.ok(data, redirectTo); | |
* return res.ok(data, true); | |
* | |
* @param {Object} data | |
* @param {Boolean|String} viewOrRedirect | |
* [optional] | |
* - pass string to render specified view | |
* - pass string with leading slash or http:// or https:// to do redirect | |
*/ | |
module.exports = function sendOK (data, viewOrRedirect) { | |
// Get access to `req` & `res` | |
var req = this.req; | |
var res = this.res; | |
// Serve JSON (with optional JSONP support) | |
function sendJSON (data) { | |
if (!data) { | |
return res.send(); | |
} | |
else { | |
if (typeof data !== 'object') { return res.send(data); } | |
var format = {}; format[req.options.model] = data; | |
if ( req.options.jsonp && !req.isSocket ) { | |
return res.jsonp(format); | |
} | |
else return res.json(format); | |
} | |
} | |
// Set status code | |
res.status(200); | |
// Log error to console | |
this.req._sails.log.verbose('Sent 200 ("OK") response'); | |
if (data) { | |
this.req._sails.log.verbose(data); | |
} | |
// Serve JSON (with optional JSONP support) | |
if (req.wantsJSON) { | |
return sendJSON(data); | |
} | |
// Make data more readable for view locals | |
var locals; | |
if (!data || typeof data !== 'object'){ | |
locals = {}; | |
} | |
else { | |
locals = data; | |
} | |
// Serve HTML view or redirect to specified URL | |
if (typeof viewOrRedirect === 'string') { | |
if (viewOrRedirect.match(/^(\/|http:\/\/|https:\/\/)/)) { | |
return res.redirect(viewOrRedirect); | |
} | |
else return res.view(viewOrRedirect, locals, function viewReady(viewErr, html) { | |
if (viewErr) return sendJSON(data); | |
else return res.send(html); | |
}); | |
} | |
else return res.view(locals, function viewReady(viewErr, html) { | |
if (viewErr) return sendJSON(data); | |
else return res.send(html); | |
}); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This helps me format my sails.js blueprints to shoot out data for ember.js It's great :D Help was given by robdubya from the #sailsjs irc