Skip to content
This repository has been archived by the owner on Dec 20, 2022. It is now read-only.

2. Modules

Dmitry Evseev edited this page Jul 9, 2014 · 3 revisions

ngseed uses a modular paradigm according to which each component should be isolated into a module.

Here is an example of project "modules" folder:

- modules/
   - common/
   - moduleA/
   - moduleB/
   - resources/
   - ui/

"common" module

The "common" module is actually a collection of small modules - a container for most used global directives, filters and services.

As a rule when project is already under development, it's likely to see there directives.js and filters.js files. Some large directives or filters should be placed into own file in common folder like common/reset-form-directive.js.

"ui" module

Just like a "common" module, this one is a container for sub-modules that solve ui/ux problems (no domain logic). For example, sub-module "ui/menu" is a standalone module that introduces menu directive.

"resources" module

Is another collection of modules intended to encapsulate communication with the backend. This approach fits nice REST APIs. Following it you should create a module with "ngResource" object (it can also be custom implementation of resource) per each API endpoint, and talk to API only through resources.

ModuleA..Z

All modules in ngseed should be defined in a certain way.

Naming

Name of a module and its files is of no less importance than the code itself. Try your best to find a good condensed name that describes exactly what the module is doing.

Modules names should be chained and separated by . (dot), i.e. we always have root module app (or your application's abbreviation which is not longer 4 symbols) and modules for calendar and settings, so their names are app.calendar and app.settings. In turn, app.settings might has app.settings.general sub-module etc.

As a rule, name of module folder should be the same:

  • app.calendarcalendar
  • app.settingssettings
  • app.home-electricityhome-electricity

Structure

Every module has:

  • index.js -- loader/entry point. It's used to include the module into another module
  • module.js -- module definition and set up of external dependencies
  • Other module components go into own files -- services, directive, filters, templates and etc
  • Sub-modules (optional)

In order...

index.js

This is a loader file. This file plays an important role but actually is a technical, its presence is necessary because of requirejs nature. In order to load one module into another we need to know its structure, components. Index.js does exactly the same - defines list of internal components this module introduces ... to be continued

module.js

TODO

Others components

TODO

Sub-modules

See sub-modules page.

Stripped-down Alternative Structure

  • index.js
  • Sub-modules (optional)