Note About Front End Development, Vue, React, Java Spring, Maven Etc.

Finally find a place cool enough to host a blog

View on GitHub
5 October 2018

Introduction to AMD Module

by libai8723

AMD是一种曾经非常主流的js module的定义,但是后来不流行了,因为Mendix的原因我这边曾经短暂的学习过一下dojo和amd

[Topic List 2 ]Introduction to AMD Module

1. After reading hello dojo, Modern dojo, dojoCondig

It seem I have a much deeper understanding about the Module, but the first exmaple code seem pretty cool, so I keep it down here

2. What is a Module

3. How to Create a Module

4. A simple module example

define(function(){
    var privateValue = 0;
    return {
        increment: function(){
            privateValue++;
        },

        decrement: function(){
            privateValue--;
        },

        getValue: function(){
            return privateValue;
        }
    };
});

the basic idea is that define an anonymous function in define function, and return a object to expose the functionality.

and this module will be contains in a javascript file, when you want to use it, use require to include it. require will load the file and pass the object as a parameter to the anonymous function in require function.

so below example show how to use a module:

<html>
    <body>
        <script src="dojo/dojo.js" data-dojo-config="async: true"></script>
        <script>
            require([
                "app/counter"
            ], function(counter){
                log(counter.getValue());
                counter.increment();
                log(counter.getValue());
                counter.decrement();
                log(counter.getValue());
            });
        </script>
    </body>
</html>

5. Modules load Modules

use this example code below:

define([
    "dojo/_base/declare",
    "dojo/dom",
    "app/dateFormatter"
], function(declare, dom, dateFormatter){
    return declare(null, {
        showDate: function(id, date){
            dom.byId(id).innerHTML = dateFormatter.format(date);
        }
    });
});
  1. in this example we show multiple dependencies, so you can see multiple modules list in the dependency list.
  2. and this module return a constructor, which i do not know, maybe defined by declard function. So definitely we should go through function declare

6. Using plguins

In addition to regular modules, the AMD loader also features a new type of module called a plugin. Plugins are used to extend the loader with new features beyond simply loading an AMD module. Plugins are loaded more or less the same way as a regular module, but use a special character “!” at the end of the module identifier to identify the request as a plugin request. Data after the “!” is passed directly to the plugin for processing.

  1. dojo/text: will load a string from a file
  2. dojo/i18n: loads language resource bundles according to the web browser’s user locale. Its usage looks like this:
  3. dojo/has: Dojo’s loader includes an implementation of the has.js feature detection API; the dojo/has plugin leverages this functionality for requiring modules conditionally.
  4. dojo/domReady: It is a module that simply doesn’t resolve until the DOM is ready.

The Last: Lists of Topic to Read

sadlly the todo list becomes longer… let us continue.

  1. 1. Asynchronous Module Definition (AMD)
  2. 4. Query and Event
  3. 5. Ajax
  4. 6. Template-based Widgets
  5. 7. Dijit Widget
  6. 8. Core Concept
  7. 9. Feature Detection
  8. 10. Advanced AMD Usage tutorial
  9. 11. The Dojo Loader: More Details
  10. 12. Creating Classes use declare
tags: Dojo