## m.module A module is an Object with two keys: `controller` and `view`. Each of those should point to a Javascript class constructor function. 'm.module' activates a module by instantiating its controller, then instantiating its view and rendering it into a root DOM element. Conceptually, the easiest way to think of a module is as a logical namespace with which to organize applications. For example, an app might have a dashboard module, a userEditForm module, an autocompleter module, a date formatting module, etc In the context of single page applications (SPA), a module can often be thought of as the code for a single "page", i.e. a visual state that is bookmarkable. Module can, however, also represent *parts* of pages. Note that a module might have external dependencies and that the dependencies aren't considered part of the module. In more complex applications, modules can be nested in a [hierarchical MVC](http://en.wikipedia.org/wiki/Hierarchical_model%E2%80%93view%E2%80%93controller) pattern. Nested reusable modules that have views are called **Components**. Modules and namespaces are often used interchangeably, but namespaces that do not implement the module interface (that is, objects that do not have a property called `controller` and a property called `view`) cannot be activated with `m.module`. For example, a namespace for date formatting utilities could be labeled a "module" (in the generic sense of the word) but it would not contain a view class, and therefore attempting to initialize it via `m.module` would result in undefined behavior. --- ### Usage You can make anonymous modules out of existing classes ```javascript //controller class var dashboardController = function() { this.greeting = "Hello"; }; //view class var dashboardView = function(ctrl) { return m("h1", ctrl.greeting); }; //initialize an anonymous module m.module(document.body, {controller: dashboardController, view: dashboardView}); ``` Typically, however, modules and namespaces are used interchangeably. ```javascript //`dashboard` is both a namespace and a module var dashboard = {} //controller class dashboard.controller = function() { this.greeting = "Hello"; }; //view class dashboard.view = function(ctrl) { return m("h1", ctrl.greeting); }; //initialize it m.module(document.body, dashboard); ``` The example below shows a component module called `user` being included in a parent module `dashboard`. ```javascript //this is a sample module var dashboard = { controller: function() { this.greeting = "Hello"; this.user = new user.controller(); }, view: function(controller) { return [ m("h1", controller.greeting), new user.view(controller.user) ]; } }; //this module is being included as a component var user = { //model User: function(name) { this.name = name; }, //controller controller: function() { this.user = new user.User("John Doe"); }, //view view: function(controller) { return m("div", controller.user.name); } }; //activate the dashboard module m.module(document.body, dashboard); ``` yields: ```markup