Doubt about loading modules with requireJS and angularJS

2

I am studying a code from a former employee of the company and he used requireJS + angularJS. It created several modules with enough services etc, however, my question is: when using a service or a directive, does the require and the angular load any module or just the calling service or policy? Because I understood the advantages of using require, but I did not understand how it works in practice.

    
asked by anonymous 16.06.2015 / 14:56

3 answers

1

Module loading depends on how the application is configured.

As initial documentation suggests :

  • In development, the loader loads the main and then each module when required.
  • After development, on the Internet, loading each module individually would completely kill the performance of a non-trivial application. So it is common to package the modules in a single larger script and add it preemptively to the page, so loader identifies that the modules are already present and runs the scripts quickly.
  • Packing scripts to optimize page load is a complicated art. You can read a bit more about this in my other response .

        
    07.07.2016 / 03:20
    0

    To make sense, think about the following:

    An application with several modules, various controllers, directives, services, etc.

    If you do not use require, you need to include all files at once in your view, using RequireJS, you can:

    By separating each module, service, controller, directive into separate files in their respective directories, they will be loaded only when necessary.

    In addition, your application becomes more organized, since you can set up an easy-to-use file structure. And its elements can be reused when needed.

        
    22.06.2015 / 22:39
    0

    Complementing with @Pedro Laini's answer I'm also using a dynamic load, using ocLazyLoad .

    It can be used in conjunction with your route system, being loaded from a resolver. So let's say you have an administrative area with permissions system, it can help even more in the security of your application, since the module will only be loaded if it passes the resolve.

    In addition, as said by Pedro Laini, as the modules will only load as needed, your initial load will be much lower, giving your app more speed.

        
    08.11.2015 / 19:55