Skip to content

Instantly share code, notes, and snippets.

@ofridagan
Created May 13, 2014 17:44
Show Gist options
  • Save ofridagan/432e81e0f534b0cfcf13 to your computer and use it in GitHub Desktop.
Save ofridagan/432e81e0f534b0cfcf13 to your computer and use it in GitHub Desktop.
Sails.js find action that returns a format for Ember
find: (req, res, next) ->
id = req.param 'id'
return next() if isShourtCut id
if id
User.findOne id, (err, user) ->
return res.notFound() if user == undefined
return next err if err
res.json user
else
where = req.param 'where'
where = JSON.parse where if _.isString where
options =
limit: req.param 'limit' || undefined
skip: req.param 'skip' || undefined
sort: req.param 'sort' || undefined
where: req.param 'where' || undefined
User.find options, (err, users) ->
return res.notFound() if users == undefined
return next err if err
res.json {users}
isShourtCut = (id) -> _.contains ["find", "create", "update", "destroy"], id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment