Skip to content

Instantly share code, notes, and snippets.

@searls
Created January 24, 2012 22:02
Show Gist options
  • Select an option

  • Save searls/1672989 to your computer and use it in GitHub Desktop.

Select an option

Save searls/1672989 to your computer and use it in GitHub Desktop.
window.mapRelations = (model,doThis,exclusions=[]) ->
result = doThis.call(model,model)
exclusions.push(model)
if model instanceof Backbone.RelationalModel && !model.isLocked()
model.acquire()
_(model.getRelations()).each (relation) =>
related = relation.related
unless _(exclusions).include(related)
if related instanceof Backbone.Collection
result[relation.key] = []
related.each (item) ->
result[relation.key].push(mapRelations(item,doThis,exclusions))
else
result[relation.key] = mapRelations(related,doThis,exclusions)
model.release()
result
describe "mapping relations", ->
context "get all the IDs", ->
Given -> @toy = new Toy
Given -> @cane = new BambooCane
Given -> @model.set toy: @toy
Given -> @model.get('bambooCanes').reset(@cane)
When -> @result = window.mapRelations(@model, (m) -> id: m.cid)
Then -> expect(@result).toEqual
id: @model.cid
toy:
id: @toy.cid
bambooCanes: [
id: @cane.cid
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment