Loading Text and JSON

The require and define functions are used to load JavaScript modules and other files that the current code can require. To load a text content from a file, you must process it using a special plugin.

Loading Plain Text

To load file content as a text, use the dojo/text! plugin as in the following example:

require([ ..., "dojo/text!./data/simpleText.txt",...],
   function(..., simpleText,...) {
      ...
});

In the above code, the text loaded from the simpleText.txt file is copied to the simpleText input parameter of the main callback function.

Loading JSON Object

A JSON definition can be loaded similar to plain text, but to use the loaded contents as a JSON object it is necessary to process it preliminarily by a JSON parser.

The aps/json! plugin processes file content as JSON code. The callback function will get a JavaScript JSON object that does not require additional parsing, for example:

define([ ..., "aps/json!./data/newvps.json",...],
   function(..., newVPS,...) {
      ...
      var vpsModel = getStateful({"data": newVPS});
      ...
});

In the above code, a stateful object is created from the newVPS JSON object directly without first converting it from text to JSON.