Search code examples
javascriptdojoamd

"dojo AMD format, makes code easier to author and debug", how to prove it?


Dojo said that "dojo AMD format, makes code easier to author and debug" (https://dojotoolkit.org/documentation/tutorials/1.10/modules_advanced/)

is there any one can show us an example code to prove this statement? tanks :)


Solution

  • AMD allows division/organization of your code in modules, which are loaded on demand, this have some advantages:

    • Organization: You code tend to be more structured and organized when you think in term of modules.
    • Debug: As you code is separated in functionalities/feature per module it simplifies debugging as the amount of code for a module is more limited in length and scope.
    • Testing: It is more easy to organize your test cases when your code it is well defined in separate modules.

    More information about AMD and module.

    Example of simple module for a navbar:

    // in "my/widget/NavBar.js"
    define([
        "dojo/_base/declare",
        "dijit/_WidgetBase",
        "dijit/_TemplatedMixin",
        "dojo/text!./templates/NavBar.html"
    ], function(declare, _WidgetBase, _TemplatedMixin, template){
        return declare([_WidgetBase, _TemplatedMixin], {
            // template contains the content of the file "my/widget/templates/NavBar.html"
            templateString: template
        });
    });