Created
October 16, 2014 18:40
-
-
Save prestonp/6b9ab37ce8673e861986 to your computer and use it in GitHub Desktop.
dirac middleware success hook
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
m.update = function(collection, options){ | |
options = options || {}; | |
return function(req, res){ | |
collection = collection || req.collection; | |
collection.update(req.queryObj, req.body, req.queryOptions, function(error, results){ | |
if (error) return console.log(error), res.status(400).send(); | |
if (results && results.length == 0) return res.status(404).end(); | |
if (!req.queryOptions.returning || req.queryOptions.returning.length == 0) | |
return res.status(204).end(); | |
var val = !options.isMultiple ? results[0] : results; | |
if ( req.queryOptions.envelope ){ | |
val = { data: val } | |
} | |
if (typeof options.success === 'function') | |
options.success(val); | |
res.status(200).json(val); | |
}) | |
}; | |
}; |
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
app.put('/api/orders/:id' | |
, m.restrict(['admin']) | |
, m.param('id') | |
, m.update( db.orders, { success: function(order) { | |
if ( order ) venter.emit('order:type:change', order); | |
}}) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment