Created
January 12, 2014 04:05
-
-
Save robwormald/8380691 to your computer and use it in GitHub Desktop.
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
/** | |
* | |
*/ | |
module.exports = function destroy (req, res) { | |
// Locate and validate id parameter | |
var id = req.param('id'); | |
if (!id) { | |
return res.badRequest('No id provided.'); | |
} | |
// Get access to sails (globals might be disabled) | |
var sails = req._sails; | |
// The name of the parameter to use for JSONP callbacks | |
var JSONP_CALLBACK_PARAM = 'callback'; | |
// if req.transport is falsy or doesn't contain the phrase "socket" | |
// and JSONP is enabled for this action, we'll say we're "isJSONPCompatible" | |
var isJSONPCompatible = req.options.jsonp && ! ( req.transport && req.transport.match(/socket/i) ); | |
// Look up the model.... | |
var Model = sails.models[req.options.model]; | |
// Otherwise, find and destroy the model in question | |
Model.findOne(id).exec(function found(err, result) { | |
// TODO: differentiate between waterline-originated validation errors | |
// and serious underlying issues | |
// TODO: Respond with badRequest if an error is encountered, w/ validation info | |
if (err) return res.serverError(err); | |
if (!result) return res.notFound(); | |
Model.destroy(id).exec(function destroyed(err) { | |
// TODO: differentiate between waterline-originated validation errors | |
// and serious underlying issues | |
// TODO: Respond with badRequest if an error is encountered, w/ validation info | |
if (err) return res.serverError(err); | |
// If 'silent' is set, don't use the built-in pubsub | |
// if (!req.options.silent) { | |
// TODO: enable pubsub in blueprints again when new syntax if fully fleshed out | |
// sails.publish(newInstance, { method: 'destroy', data: newInstance.toJSON }); | |
// } | |
// Respond with JSON or JSONP | |
if ( isJSONPCompatible ) { | |
return res.jsonp(result); | |
} | |
else { | |
return res.json(result); | |
} | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment