I have a model that needs to be accessed through several views, and for this, in the definition of the model module, I create it right away:
define([ 'jquery', 'underscore', 'backbone' ], function(_, Backbone) { var Foo = Backbone.Model.extend({
I really only need one instance of this model - right now. 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 create and store links to objects when the application starts:
require([ 'App', 'Foo' ], function(App, Foo) { App.models.foo = new Foo(); });
but I feel this is a bad alternative, since you are essentially reverting to the global namespace - this is what RequireJS should avoid.
Are there any alternatives and are there good reasons to avoid using singleton models as described above?
Radu
source share