What would be the best way to ensure that my collection stays cached and thereby only gets fetched once?
Should I implement some sort of cache layer? Should I share the
I ended up doing something similar to ProTom's solution. Instead of dynamically initializing a collection based on the name, I decided to just use a function to setup the collection. Through my application, I needed to initialize collections differently depending on where it was coming from. This proved to be the best way for my needs. Here's the CoffeeScript:
Cache:
cachedCollections: {}
getCollection: (key, block) ->
collection = @cachedCollections[key]
return collection if collection
collection = block()
@cachedCollections[key] = collection
collection
Usage:
commentCollection = getCollection "comments-#{postId}", ->
collection = new CommentCollection
collection.url = "/api/posts/#{postId}/comments"
collection