Skip to content

Instantly share code, notes, and snippets.

@otang
Created August 15, 2013 23:07
Show Gist options
  • Save otang/6245793 to your computer and use it in GitHub Desktop.
Save otang/6245793 to your computer and use it in GitHub Desktop.
# Retrieves the root pages and their children (level 0 and level 1)
exports.getPages = (req, res) ->
getChildren = (page, callback) ->
models.Page.find({ parent_id: page._id }).sort({ name: 'asc' }).exec (err, children) ->
page.children = children || []
callback(null, page)
models.Page.find({ parent_id: null }).sort({ name: 'asc' }).exec (err, pages) ->
async.map pages, getChildren, (err, results) ->
res.json results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment