Search code examples
javascriptrequirejsamd

Requirejs and load multiple libraries in module


How can I load multiple amd libraries in requirejs. For example, I've many modules, having below code:

define(["jquery", "backbone", "underscore", "handlebars"], 
    function ($, Backbone, _, Handlebars) {
        ...
        // code
        ...
})

How i can load all necessary libraries in module easier(may be as a single library)?


Solution

  • It is not really in the requirejs philosophy, but you can make a module including the library and put them into an object like this :

    // myModule.js
    define(["jquery", "backbone", "underscore", "handlebars"], function ($, Backbone, _, Handlebars) {
        return {
            $ : $,
            Backbone : Backbone,
            _ : _,
            Handlebars : Handlebars
        };
    });
    
    // Into an other file
    define(['myModule'], function(myModule) {
        myModule.Backbone.Model({ ... });
    });