Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Allow passing a custom seek method and a URL base
Browse files Browse the repository at this point in the history
  • Loading branch information
bergie committed Mar 6, 2012
1 parent cf7faeb commit 6dbc6ad
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/resource-juggling.coffee
Expand Up @@ -23,7 +23,7 @@ exports.getResource = (options) ->

options.toJSON ?= (items, req) -> items

seek = (request, constraints, callback) ->
options.seek ?= (request, constraints, callback) ->
if collectionIsRelation
collection = options.collection
if typeof options.collection is 'function'
Expand All @@ -35,6 +35,11 @@ exports.getResource = (options) ->
callback err, items
return

if constraints.id
options.model.find constraints.id, (err, item) ->
callback err, [item]
return

options.model.all
where: constraints
, (err, items) ->
Expand All @@ -47,13 +52,13 @@ exports.getResource = (options) ->
load: (request, where, callback) ->
whereQuery = {}
whereQuery[options.where] = where
seek request, whereQuery, (err, items) ->
options.seek request, whereQuery, (err, items) ->
return callback err if err
callback null, items[0]

index:
html: (req, res) ->
seek req, {}, (err, items) ->
options.seek req, {}, (err, items) ->
if options.addPlaceholderForEmpty and items.length is 0
blankItem = {}
for property, defs of options.schema.definitions[options.name].properties
Expand All @@ -66,7 +71,7 @@ exports.getResource = (options) ->
items: items
as: 'item'
json: (req, res) ->
seek req, {}, (err, items) ->
options.seek req, {}, (err, items) ->
res.send options.toJSON items, req
default: exports.contentNegotiator

Expand Down Expand Up @@ -98,3 +103,6 @@ exports.getResource = (options) ->
json: (req, res) ->
res.send options.toJSON req[options.urlName], req
default: exports.contentNegotiator

resource.base = options.base if options.base
resource

0 comments on commit 6dbc6ad

Please sign in to comment.