A
A
Anton Medvedev2012-07-11 18:25:12
JavaScript
Anton Medvedev, 2012-07-11 18:25:12

How to organize a large number of dependencies in RequireJS?

I'm using RequireJS and ran into this problem:

define([ "require", "jquery", "blade/object", "blade/fn", "rdapi",
         "oauth", "blade/jig", "blade/url", "dispatch", "accounts",
         "storage", "services", "widgets/AccountPanel", "widgets/TabButton",
         "widgets/AddAccount", "less", "osTheme", "jquery-ui-1.8.7.min",
         "jquery.textOverflow"],
function (require,   $,        object,         fn,         rdapi,
          oauth,   jig,         url,         dispatch,   accounts,
          storage,   services,   AccountPanel,           TabButton,
          AddAccount,           less,   osTheme) {

});

A lot of dependencies.

What to do to avoid this? Ideally, I would like to simply write at the beginning of the file:
Module = require('module')
User = require('user')
...

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Zhurbitsky, 2012-07-11
@blo

Your example is from the documentation, it also describes how to make it a little more readable

define(['require', 'dependency1', 'dependency2'], function (require) {
    var dependency1 = require('dependency1'),
        dependency2 = require('dependency2');

    return function () {};
});

P
Pavel Ivannikov, 2014-06-09
@Faustlogger

Personally, I describe dependencies for modules in the config at the requirejs entry point :

require.config({
// Описываем пути
    baseUrl: '../js'
    paths: {
        jquery: 'libs/jquery',
        someModule: 'module'
    },
// Описываем зависимости для модулей
    shim: {
        someModule: ['jquery']
    }
});

As a result, in the plug-in, I do not need to specify dependencies
define(function(){
    ...
    obj.someFunc = function(){
        $("#someButton").click(...)
    }
    return obj;
});

Verified personally. Works. Here is the link to the documentation

K
Konstantin Kitmanov, 2012-07-12
@k12th

define(
    'blade',
    ['blade/object', 'blade/fn', 'blade/jig', 'blade/url'],
    function(object, fn, jig, url) {
        return {
            object: object,
            fn: fn,
            jig: jig,
            url: url
        } 
    }
)

// …

define(
    ['blade', 'require', 'jquery'],
    function(blade, require, jquery) {
        // …
    }

)

That is, we collect into packages according to namespaces and we already connect the packages.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question