AMD – The Asynchronous Module Definition

A very brief introduction into AMD. AMD – the heart of DoJo since version 1.7.0. AMD is design pattern based on modules & its dependencies can be run, downloaded or loaded asynchronously. I like the idea of loading stuff asynchronously. It makes things much more faster, than synchronous way.

AMD = Asynchronous

Asynchronous means, it can be loaded basically parallelly. Loading of the one script does not need to wait for the another script to be loaded. It is just that simple. Scripts are loaded by the Loader.

AMD = Module

Modularity. Big issue of JavaScript. AMD design pattern uses power of JavaScript to create new approach of writing Web Apps. AMD brings a huge pros, namely the ability to define its modules in a practical and reusable way.

Define();

An important function is define. Function define can take up to three parameters:

First parameter “id” is a string literal. “Id” specifies namespace and name of new module. It is name of the class with its namespace. If you did not specify “id”, which is optional. It is specified from file path.

The dependencies parameter is another optional parameter. It is an array literal. It’s the list of other classes which are needed to include, into our new class.

The factory parameter is the logic of your class. It is necessary part of define function.

This is a short example of define function:

Require();

When you defined new classes, you should connect all stuff you need for your application by using “require()” function. You are using class by this function. It is very similar as define function. It needs only dependencies parameter, if there are some. And the logic – a factory.
This is short example of require function:

 

Please tell what you think