AMD (Asynchronous Module Definition) “The Asynchronous Module Definition (AMD) API specifies a mechanism for defining modules such that the module and its dependencies can be asynchronously loaded."
AMD - define() - [dependencies] ● Array literal of the required modules. ● Are passed to the factory function as parameters (in same order). define(‘ironMan’ , [‘stark.tony’, ‘ego.massive’, ‘suit’] , function(Tony, Ego, Suit) {});
AMD - define() - factory ● Function that should be executed to instantiate the module or object. ● Only executed once. define(‘ironMan’ , [‘stark.tony’, ‘ego.massive’, ‘suit’] , function(Tony, Ego, Suit) { return Tony.apply(Ego).don(Suit); });
Definition: module ● Self-contained implementation of a website component. ● Initialization of event bindings. ● Instantiates and glues MV* components.
Module Communication ● Events (pub / sub via mediator) ○ Passive! ○ Past tense! ○ Verb + target (“found:theAnswer”, “added:bookmark”) ● Public APIs (accessible via dependency) ○ imperative actions (tellUsTheAnswer)
What is MV* ● Separation of concerns. ● Most modules implementations include: ○ Rendering / updating user interfaces. ○ Handling user input. ○ Server communication. ● MV* is too simplistic when applied to large- scale JS architecture, however, when applied to a module….
Backbone ● “Magic library” for structuring web applications. ● Not a full-stack framework, more “convention than code”. ● Data is represented as a model. ● Views react to changes in models and user interaction.
Underscore ● Utility library for Javascript. ● Does not extend any built-in JS objects. ● Uses “_” as a global alias. ● Functions map to native implementations where possible.
AMD + Backbone ● Can use AMD to define MV* components (or just define views in module) ● The factory function should return the extended prototype of Backbone.* ● Instances are created with new keyword.
Improvements ● Sandboxing: use one central dependency with message bus for module communication. ● Standardized module definition: all modules contain references to the app instance and other shared resouces.