Wednesday, 4 April 2012

Dojo: version 1.7 and the AMD loader

Along with a few other JavaScript libraries, Dojo is making the transition to the Asynchronous Module Definition (AMD) API. This post presents a few minimal files written to make use of it.

Loading Hello World!

Here's a simple "Hello World!" example:

/* hello/world.js */
define([], function () {
   "use strict";
   return "Hello, World!";
});

And since no code is complete without a unit test, here is a Dojo Objective Harness (DOH) example showing how to consume this "hello/world":

/* hello/tests/worldTests.js */
define(["doh", "hello/world"], function(doh, helloworld) {
    "use strict";
    doh.register("hello.test.worldTests", [{
        name: "helloTest",
        runTest: function(){
            doh.assertEqual("Hello, World!", helloworld);
        }
    }]);
});

Declaring Dojo types

Here is an example of how to declare a Dojo type:

/* hello/Alpha.js */
define(["dojo/_base/declare"], function (declare) {
   "use strict";
   return declare("hello.Alpha", [], {
       sum : function (n1, n2) {
           // summary
           //       Adds two numbers
           // n1: Number
           // n2: Number
           // returns: Number
           return n1 + n2;
       }
   });
});

And the unit test:

/* hello/tests/AlphaTests.js */
define(["doh", "hello/Alpha"], function (doh, Alpha) {
    "use strict";
    doh.register("hello.tests.AlphaTests", [{
        name: "helloTest",
        runTest: function() {
            var alpha = new Alpha();
            doh.assertEqual(2, alpha.sum(1, 1));
        }
    }])
});

Inheritance

Type inheritance has not changed much since 1.6; here hello.Beta is a subtype of hello.Alpha:

/* hello/Beta.js */
define(["dojo/_base/declare", "hello/Alpha"], function (declare, Alpha) {
   "use strict";
   return declare("hello.Beta", [Alpha], {
       negativeSum : function (n1, n2) {
           return -1 * this.sum(n1, n2);
       }
   });
});

The unit test:

/* hello/tests/AlphaTests.js */
define(["doh", "hello/Beta"], function (doh, Beta) {
    "use strict";
    doh.register("hello.tests.BetaTests", [{
        name: "helloTest",
        runTest: function() {
            var beta = new Beta();
            doh.assertEqual(-2, beta.negativeSum(1, 1));
        }
    }])
});

Running the DOH unit tests

The project directory structure:

dojo-release-1.7.2-src\<...Dojo files...>
hello\Alpha.js
hello\Beta.js
hello\tests
hello\world.js
hello\tests\AlphaTests.js
hello\tests\BetaTests.js
hello\tests\module.js
hello\tests\worldTests.js
        

The test module:

/* hello/tests/module.js */
define(["hello/tests/worldTests",
    "hello/tests/AlphaTests",
    "hello/tests/BetaTests"], 1);

This module can be run by firing up a HTTP server and using this relative URL:

./dojo-release-1.7.2-src/util/doh/runner.html?testModule=hello.tests.module&paths=hello,../../hello

Individual tests can be run by modifying the testModule parameter.

End notes

The Dojo documentation is in flux. The main tutorials have been updated to 1.7, but much of the other documentation is lacking. Besides the code itself, the livedocs seem to be in relatively good shape.

No comments:

Post a Comment

All comments are moderated