Ladislav M
Ladislav M

Reputation: 2187

More than one modul per file - RequireJS and Backbone

I'm now using Backbone without RequireJS and I'd like to rewrite whole my app to utilize RequireJS benefits. Only thing that holds me back is the notion that I will have move each Backbone's Model/View/Collection into separated file to create modules. I would prefer to have more than only one module per file, so the relations for example between linked Models and Views could stay more evident.

Upvotes: 0

Views: 123

Answers (1)

Nazar
Nazar

Reputation: 1799

You can have more than one instances in one module:

define(function ( require ) {

    var FirstModel = Backbone.Model.extend({
        // Logic
    });

    var SecondModel = Backbone.Model.extend({
        // Logic
    });

    return {
        first: FirstModel,
        second: SecondModel
    };

});

And then simply use them:

define(function ( require ) {

    var myModels = require( 'path/to/module' );

    // use myModels.first and myModels.second here

});

Upvotes: 2

Related Questions