Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

model loader - when setting collection.url wrap call to each collecti…

…ons url

function in a closure

fixes issue where the url for the last collection defined in the collectionTypes
object would be requested Object.keys(collectionTypes).length times
  • Loading branch information...
commit 69f5d5abcc2604272b97d42d4705081053180eec 1 parent ad8f48b
Nat Lownes natlownes authored
Showing with 32 additions and 1 deletion.
  1. +1 −1  models/loader.coffee
  2. +31 −0 test/models/loader_spec.coffee
2  models/loader.coffee
View
@@ -14,7 +14,7 @@ module.exports =
collection = new collectionType
if $.isFunction(collection.url)
urlFunc = collection.url
- collection.url = ->
+ collection.url = do ->
apiRoot + urlFunc()
else
collection.url = apiRoot + collection.url
31 test/models/loader_spec.coffee
View
@@ -40,6 +40,37 @@ describe 'Model Loader', ->
load(collections, 'api-root').done ->
done()
+ describe 'when loading multiple collections with url functions', ->
+
+ it "should make a request to each collection's url", (done) ->
+ collectionOneFetches = 0
+ collectionTwoFetches = 0
+
+ test.when 'GET', 'api-root/my/mock/func/url1', (req) ->
+ collectionOneFetches++
+ status: 200
+
+ test.when 'GET', 'api-root/my/mock/func/url2', (req) ->
+ collectionTwoFetches++
+ status: 200
+
+ class FuncOneCollection extends Collection
+ model: MockModel
+ url: -> '/my/mock/func/url1'
+
+ class FuncTwoCollection extends Collection
+ model: MockModel
+ url: -> '/my/mock/func/url2'
+
+ collections =
+ funcOne: FuncOneCollection
+ funcTwo: FuncTwoCollection
+
+ load(collections, 'api-root').done ->
+ expect(collectionOneFetches).to.equal 1
+ expect(collectionTwoFetches).to.equal 1
+ done()
+
it 'should not fetch lazy collections', (done) ->
fetches = 0
Please sign in to comment.
Something went wrong with that request. Please try again.