简体   繁体   中英

When to instantiate single instance backbone model using requirejs

I am using RequireJS with backbone and I am wondering when should I return an instance and when I should return a class definition.

For example, I have a model that I need only one instance of it and I need only one instance for it. Should I instantiate it in the RequireJS model module or in the appView initialize?

  define([
        'Underscore',
        'Backbone'
    ], function(_, Backbone) {
        var TermModel = Backbone.Model.extend({
            defaults: {
                term: ''
            }
        });


return new TermModel(); //<-----------new OR not??
});

First you should shim underscore in the config file, check this for reference http://requirejs.org/docs/api.html#config

Second, if you need to instanciate this model only one time then this is fine. Though keep in mind you will have less control over the instantiation time, therefore I wouldn't recommand it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM