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
2 Answers
Reset to default 5Hmm.. 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
版权声明:本文标题:javascript - Singleton Backbone models with RequireJS: anti-pattern? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741671658a2391639.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论