admin管理员组

文章数量:1302408

I have a model that needs to be accessed by several views and to acplish 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?

I have a model that needs to be accessed by several views and to acplish 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?

Share Improve this question asked Sep 21, 2012 at 3:03 RaduRadu 8,6998 gold badges56 silver badges93 bronze badges 1
  • You should be pletely fine they way you have done it in your first example. – Radko Dinev Commented Aug 27, 2014 at 7:25
Add a ment  | 

2 Answers 2

Reset to default 5

Hmm.. I have been using the RequireJS modules as Singleton objects for a while with no problem. Here is a related question that I asked.

Is it a bad practice to use the requireJS module as a singleton?

Hope this helps!

You don't need to create the namespace thing. Your first example creates a singleton. Whenever you require this module you get the same instance of your model. So instead of creating a new App module and save the instance there, just require the module of your first example directly. We use it in our app to have a singleton instance of our app and I can see no pitfalls with this.

本文标签: javascriptSingleton Backbone models with RequireJS antipatternStack Overflow