Naor
Naor

Reputation: 24063

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??
});

Upvotes: 0

Views: 505

Answers (1)

daouzli
daouzli

Reputation: 15328

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.

Upvotes: 1

Related Questions