Home

Awesome

angular-require-lazy

An example-experimental application for mixing AngularJS, RequireJS and require-lazy.

Changes

Preface

This is an example application for keeping track of personal/family expenses. This is still work in progress and open for discussion. Its main purpose is to demonstrate the mixing of Angular and require-lazy, not to be functionally complete, at least at this stage.

Requirements

  1. Node.js to run the mock server and the build scripts. The node executable must be in PATH for the build scripts to work.
  2. A browser with decent debugging and network traffic analysis capabilities (e.g. latest Firefox+Firebug).

Run

  1. Clone the GIT repository
  2. npm instal
  3. bower install
  4. node app.js to run the server
  5. Hit http://localhost:8110/app.html (this is the unbuilt-development version of the application)

Build, test and run

(Steps 1, 2 & 3 above are prerequisites)

(NOTE: There used to be a standalone script based build system; this was deprecated and removed in favor of Grunt.)

Using Grunt

  1. Make sure grunt-cli is installed and the grunt command is in PATH.
  2. grunt to compile everything
  3. (make sure server is running → node app.js)
  4. Hit http://localhost:8110/app-built.html (this is the built version of the application)

Testing (with Karma/Jasmine)

Run karma start --single-run.

What happens?

This application is set up to load only the scripts it needs for the current view. When the view changes, only the scripts necessary for the new view are loaded.

To observe this behaviour open Firebug to the network tab, then load the unbuilt-development version of the application. Observe the scripts being loaded: ng-grid-XXX.js is not needed and not loaded. Navigate to the "Expenses" view (top menu). ng-grid-XXX.js and the application scripts required for this view are loaded just in time.

The scripts needed for each view are packed together into "bundle" files, when the application is built. So for the expenses view app/modules/expenses/main.js, app/modules/expenses/expenses-view.js, app/modules/expenses/expenses-template.html, and ng-grid-2.0.7-debug.js are bundled into one script file (i.e. expenses-built.js) and the file is loaded with a cache-breaking hash (something like a ?v=e1974633ea3017db85324f449bc6479f request parameter). This process is using r.js and require-lazy.

The noteworthy points are:

How?

Testing - Coverage - Sonar

Run grunt test to run the test suite.

Run grunt coverage to make the test coverage report. The output will be in build-coverage/report/output/lcov.info and the HTML version under build-coverage/report/output/lcov-report/index.html.

To have Sonar analyze the project, execute the coverage report first and then, run sonar-runner (make sure it is in the PATH).

Why?

I believe both RequireJS and AngularJS are very useful libraries/frameworks. I am surprized that Angular does not cooperate smoothly with RequireJS out of the box, so I have been experimenting with this implementation.

I believe this can be further improved and I hope it will contribute to a solution bridging the worlds of AMD and Angular modules.

The providers capturing technique (implemented in scripts/lib/angular-require-lazy/lazyAngularUtils.js) is based heavily on angularjs-requirejs-lazy-controllers.