I am working on a big Backbone.js application. The code is modular structured using require.js.
Now I see a lot of backbone code and tutorials doing this:
window.app = ( window.app || {} );
after they will assign model definitions and Collection instances to that global object, like so:
Task = Backbone.Model.extend({ /*...*/ });
Tasks = Backbone.Collection.extend({ /*...*/ });
window.app.Task = Task;
window.app.Tasks = new Tasks();
// do this with all your models and collections
I like this approach for its simplicity and for not having to deal with where and when to instantiate a collection. But it somehow seems wrong to first separate the code into tiny bits using require.js and after assign all together to a global variable (apart from that global variables are generally bad codestyle in javascript).
So what is your take on this, what are the pros and cons of this approach and how do you deal with your objects in Backbone?