O
O
Oleg Serykh2014-08-19 09:55:02
JavaScript
Oleg Serykh, 2014-08-19 09:55:02

RequireJS, how to solve the problem with dependent modules?

So, there is a schematic code:

//RequireJS 2.1.14
 
define('UI', ['API'], function (API) { // файл UI.js
        //API - object
});
define('API', ['UI'], function (UI) { // файл API.js
        //UI - undefined
});
 
require(['UI', 'API'], function(UI, API) {}); // вызываем все из файла min.'s

From the example, there is an attempt to use modules connected to each other, however, in the API module, the UI module will not be defined. Because RequireJS loads libs in the order specified in require(['UI', 'API'].
Is there a way to overcome this problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Fr3nzy, 2014-08-19
@seryh

Your problem is that both modules depend on each other.
In general, this is not entirely correct (architecturally).
See this question on Stackoverflow
stackoverflow.com/questions/4881059/how-to-handle-...

_
_ _, 2014-08-19
@AMar4enko

@Fr3nzy rightly pointed out that the existence of such a dependency most often points to bad architecture. Anyway, in the RequireJS manual there is this section requirejs.org/docs/api.html#circular

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question