C
C
ChillyProud2015-03-26 12:34:13
JavaScript
ChillyProud, 2015-03-26 12:34:13

How does Require.JS work?

Please explain how require.js works, what happens under the hood?
Does it mean that Angular dependency injection essentially does the same thing?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Arthur, 2015-03-26
Mudrick @mudrick

If you are using AngularJS then you don't need RequrieJS at all.
1) If you are making a bold client (i.e. minifying all scripts into one or two files, for example: components.js (which contains AngularJS , LoDash.js and other libraries / plugins), application.js (actually, your application: controllers, directives, etc.) and templates.js (all HTML templates for directives and pages)), then you don’t need RequireJS at all - it’s simply useless, since you no longer need to load modules asynchronously, because you already have everything , and Dependency Injection is already in Angular out of the box.
2) If you use Angular and also want to use RequireJS for Dependency Injection , then this, of course, is nonsense, don’t do this - Angular already has everything for DI. 3) If you need asynchronous loading of modules, then use the asynchronous loader, without DI functionality (for example, https://github.com/ded/script.js , yes, thousands of them) + Angular angular-loader.js , which allows load Angular modules in any order.

K
Konstantin Kitmanov, 2015-03-26
@k12th

Loading modules and DI are completely different things. Roughly speaking (very roughly), module loading is responsible for working with files, and DI is responsible for creating entities. require.js loads the file with your controller so that it appears in your application at all, and Angular DI is responsible for getting it the entities it needs to work.
What require.js does: according to the config and settings, it finds out the path to the file responsible for the given module, creates, if necessary, a script node with src equal to this path, and waits for this script to load.

A
Alexey Terekhin, 2015-03-26
@daron666

Requirer injects classes.
Angular injects instances.
Here is a sensible presentation on slides.com/thomasburleson/using-requirejs-with-ang...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question