RequireJS/AMD Module Forms

The CommonJS AMD proposal defines an elegant, simple API for declaring modules that can be used with synchronous or asynchronous script-tag based loading in the browser. RequireJS already implements this API, and Dojo will soon have full support as well. The API for defining modules is as simple as:

define(, , );

This simple API can be used in a variety of different ways for different situations.

Continue reading

Run-Anywhere JavaScript Modules Boilerplate Code

This entry is part 5 of 12 in the series Server-Side JavaScript, Pintura, and Persevere 2.0

For developers that are creating libraries and modules, it is generally preferable to make your code available to as broad of range of users as possible. There are several different module formats in JavaScript (a module is an encapsulation of code that draws on other modules). Picking one format is often mutually exclusive to the other formats. However, in this post I want to demonstrate how you can write JavaScript modules that can be used with multiple module loaders using some simple boilerplate. Of course not all the module loaders necessarily make sense for all modules. If you are writing a module that relies on the Node file system API, it only needs it to work with the NodeJS/CommonJS module format. Likewise, a DOM-based module wouldn’t need to run on Node.

Here we’ll deal with the actual module format, the mechanism of specifying dependencies and exporting or returning functions from the module that can be used by the users. This does not deal with the normalization of the actual APIs of the underlying system, although you might want to take a look at promised-io if you would like normalization of IO interaction across the browser, Node, and Rhino/Narwhal.

Continue reading

Understanding dojo.require

Dojo provides a feature-rich system for including JavaScript modules. Before we begin this journey to explore this concept in depth, you should know that absolutely no knowledge of the Dojo module, packaging, and build system are required to use Dojo.

You can easily get started using Dojo by using a script element referring to a copy of Dojo on the AOL or Google CDNs. If you want to host your own version of Dojo, you can easily download dojo.js, include it in a web page using a script element, and be off and running with Dojo Base.

For those new to Dojo, the following resources give a quick overview of Dojo Base:

In general, dojo.js is a lot like jquery.js or prototype js: you get a competitive set of features founds in most JavaScript libraries that are essential for building great web applications. Those features include:

  • JavaScript Language Helpers
  • Object utilities
  • Array utilities
  • DOM Manipulation
  • A normalized event system
  • Ajax & Cross domain requests
  • JSON utilities
  • Simple effects
  • Browser sniffing

Continue reading