Created
January 24, 2012 22:02
-
-
Save searls/1672989 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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 | |
This file contains hidden or 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
| 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