0

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

2
  • I guess you need to define all modules in every module you want to use them Commented Oct 16, 2015 at 14:38
  • You can't load it any easier than what you posted. Even if you manage to hack it, it will go against the proper use of modular design. Before you code something, you list what the code depends on. If you were to create some code that loads all dependencies "magically" - that's a bit of a cabbage code. Basically, you don't really have a problem. This is how it's done. Commented Oct 16, 2015 at 14:39

1 Answer 1

2

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({ ... });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.