The SDK is a fast evolving library with frequent updates. Even though we advise developers to keep up-to-date and consume the latest available release, it is not always possible. In the worse case, different components on a single page might require different versions of the SDK to work properly.
Fortunately, the AMD syntax has all the mechanism in place to allow multiple releases of a library. It simply requires some discipline from the developers and have them follow a simple set of rules. When this is properly implemented, multiple releases of the SDK can co-exist in the same page. They are then referenced by different root names, coming from different server contexts.
Here is an example:
require([sbt/dom], function(dom) {...});
require([mysbt/dom], function(dom) {...});
Two version of the same library are being used here, sbt and mysbt. They both provide a dom module.
Here is the set of rules to observe to make it possible:
1- Don't name a module in its define() statement
The actual module name is defined by the relative path from the server context path.
2- Use relative paths when accessible a module in the same library
Similary to directories, '.' and '..' can be used to access modules using relative paths.
As an example, suppose that sbt/xpath requires sbt/xml, then the following syntax should be preferred:
define(['./xml'], function(dom) {...});
instead of
define(['sbt/xml'], function(dom) {...});
Then the modules can be safely relocated from sbt to mysbt, as there is no hard coded references to sbt.
3- Don't name classes when using declare()
The SDK uses the declare() syntax from dojo to create pseudo JavaScript classes. In order to make the code relocatable, then the classes should be declared anonymous within their own AMD module. Then, the classes is accessed through its module name.
Here is an example:
// MyModule.js definition
define(['./xml','./xpath'], function(dom,xpath) {...}
var myClass = declare(null, {
...
});
return myClass;
);
// MyModule.js consumption
require([./MyModule'], function (Module) {
var m = new MyModule();
...
}
4- Consuming dijits
When consuming a dijit from the SDK in your code, refer to the module name as opposed to the class name. Actually, class names won't work as all the class are anonymous:
<div data-dojo-type="sbt/controls/grid/connections/CommunityGrid"></div>
instead of
<div data-dojo-type="sbt.controls.grid.connections.CommunityGrid"></div>
When developing a new dijit, the developer should make sure that the AMD module returns the class, as shown in 3-
5- Dojo 1.4-1.6
The AMD loader provided by the SDK overrides the default dojo.getObject() function. The new implementation first check if the requested name contains a slash '/'. If so, then it returns the result of the corresponding AMD module. Else, it default to the legacy behavior.
References:
http://dojotoolkit.org/documentation/tutorials/1.8/modules/