Is it best to place it in the global object or within a View instance?
(i’m thinking that if I place it on the global such as: window.todos = new Todos(), it would pollute the global object if there are a lot of collections; however, putting it inside a view may make things a bit confusing)
Based on your experience, where is the best way to place it?
I think this really depends on your application. What I have generally done is to establish a namespace for the application, and put collections there, as well as views:
For a large application, you might go further, establishing sub-namespaces for model classes, view classes, router classes, and application instances. There are a few advantages to this, though none of them is really crucial:
Having collections available in the
appnamespace allows them to be referenced from multiple views if necessaryWhen debugging, you have access to the collections in the console
Establishing a new namespace for your application allows you to add a number of new global variables without polluting the global namespace or worrying about bad interactions with other libraries
The last point is a really good argument for namespacing your app, but it doesn’t necessarily follow that your collections need to be at the top level of your namespace, or available globally at all. The main question is how important it is for you to be able to access the collection outside a particular parent view (or parent model). If you’re sure the collection is a one-off, it might be cleaner to keep it out of the app namespace entirely.