I have a model that needs to be accessed by several views and to accomplish this in the definition of the model module I'm instantiating it immediately like so:
define([
'jquery',
'underscore',
'backbone'
], function(_, Backbone) {
var Foo = Backbone.Model.extend({
// wondrous methods and properties
});
return new Foo();
});
I only really need one instance of this model - right now that is. The workaround for this as far as I know is to have a separate App
module. Something like:
define([], function() {
var App = {
routers: {},
models: {},
views: {}
};
return App;
});
on which you can instantiate and store references to objects on app startup:
require([
'App',
'Foo'
], function(App, Foo) {
App.models.foo = new Foo();
});
but I feel like this is a poor alternative since you're essentially going back to having a global namespace - which is something that RequireJS is supposed to help avoid.
Are there any alternatives and is there any good reason to avoid having singleton models as I described above?