How do I include a javascript library in my dojo module

  • Page Owner: Not Set
  • Last Reviewed: 2018-10-26

I'm writing my own dojo module and and I need to utilize a separate javascript library. But I thought you could only load in other modules in your define().


Answer

From the dojotoolkit site: https://dojotoolkit.org/documentation/tutorials/1.10/modules_advanced/

the AMD loader can also be used to load non-AMD code by passing an identifier that is actually a path to a JavaScript file. The loader identifies these special identifiers in one of three ways:

The identifier starts with a “/”
The identifier starts with a protocol (e.g. “http:”, “https:”)
The identifier ends with “.js”

When arbitrary code is loaded as a module, the module’s resolved value is undefined; you will need to directly access whatever code was defined globally by the script.

So what that means is that if you load in a path with one of those three identifiers it will load in your javascript file but in order to access that you need to directly call the global object in the code.

Here is an example of how I loaded in the library spacetime:

define([
	"dojo/_base/array",
	"dojo/_base/connect",
	"dojo/_base/declare",
	"dojo/_base/lang",

	"dijit/_CssStateMixin",
	"dijit/_Widget",
	"dijit/_TemplatedMixin",
	"dijit/_WidgetsInTemplateMixin",
	"dijit/dijit", // loads the optimized dijit layer
	"epi/shell/widget/DateTimeSelectorDropDown",
	"epi/epi",
	"/ClientResources/Scripts/spacetime.min.js"
],
	function (
		array,
		connect,
		declare,
		lang,
		_CssStateMixin,
		_Widget,
		_TemplatedMixin,
		_WidgetsInTemplateMixin,
		dijit,
		DateTextBox,
		epi,
		spacetime
	) {

You can see that I load the path to the library and then I call the global spacetime object. Which I found by looking at the unminified javascript.

Now you have access to that spacetime object in your dojo code and any functions related to it.