I’m trying to figure out the “best practice” solution for an issue while I’m having while structuring a web app with RequireJS and BackboneJS.
My app needs to (at start up or soon there-after) scrub a service for the majority of its data, and then parse through that data.
Obviously I want my Model to grab the data and parse it (into multiple sub-models), but where I’m stumbling a bit is how to have my models remain persistent. BackboneJS makes it feel like models should be instantiated on-the-fly with a View, but that won’t work in my scenario.
Additionally, since RequireJS isolates everything into modules outside the global namespace, I’m struggling with how to make my persistent models available to my views – should I just use the Singleton Pattern? If so, are there any suggestions on how to avoid the circular dependency problem w/ RequireJS?
My solution to this is to create a module that creates a simple object that I pass around with require.js to all my other modules.
for example, create yourobject.js and include it in all your modules. Inside yourobject.js, just create and return an object.
Then just include this module in any other module and you can use YourObj as if it was a global object (in a sense). You can save instantiated backbone views to it, the backbone router for future manipulation… whatever you like.
Just think of RequireJS as providing a nice umbrella over your code. There’s no global namespace pollution with RequireJS but that doesn’t prevent you from passing around an object like that as if it was a global. Hope this clears things up a bit!