← Back to Upcase

Passing options to Backbone collection fetch?


(Jamie Folsom) #1

Hey @joshclayton,

Wondering if you can assist on something on the BB app we worked on yesterday.

As you may recall, we’re calling Collection.fetch() onAllEvents. Something I neglected to mention is that since the JSON API is an external one, we need to pass some options to the Collection to make that fetch work (a JWT, and some other data, all contained in our existing annotations_context variable).

Is it best to do that in the initializer for the collection, or in the base.js app setup? Here’s a link to the commit with the relevant files.

Thanks!


(Josh Clayton) #2

Hi @jamiefolsom! One option is passing in the configuration; the other is to create a singleton configuration object on the application namespace (Sidebar) and refer to that config from within your Backbone collection. If you pass in the config, it’ll be more flexible if configuration is likely to change, but if you’re confident those settings would be used across multiple collections, referring to a global config object should be fine.

You’ll want to set up the configuration before you instantiate the collection, just to make sure all the settings are correct. Let me know if you have any questions!


(Jamie Folsom) #3

Hey Josh, Dan and I paired on this, and ended up doing exactly that – we’re passing in the config to the collection, and it’s working great. Since the annotator and the sidebar are separate components, makes sense that they both need the config. Thanks!