13

I am really a newbie to dojo but as i started developing a new application with dojo version 1.7.2 i also wanted to use the new AMD syntax for functions. Unfortunately i don't seem to get it. :-(

What annoys me most is that i can't simply call any function which is inside of a "require"-block. For example i have a page which on opening creates a dynamic table with several widgets in each row. Then i have a button which adds one empty row each time pressed.

Without AMD syntax it would be easy:
- put all my "dojo.require()" in the HEAD
- and then create a bunch of my own functions for creating the table and widgets
- the add row function could easily access any global-variables my previous function filled

But with AMD its like this:

Initial function creates the table and widgets:

function fillReportTable(repId) {
require(["dojo/dom-construct", "dojo/dom-attr", "dijit/form/FilteringSelect",
"dojo/data/ItemFileReadStore", "dijit/form/ComboBox", "dijit/form/DateTextBox", "dijit/form/Select", "dojo/store/Memory"],
     function (domConstruct, domAttr, FilteringSelect, ItemFileReadStore, ComboBox, DateTextBox, Select, Memory) {
   // a lot of code to create the table, consisting of SEVERAL functions 
   function createNewRow(tbl) { ...} 
   function function1 () {... } 
   function function2 () {... } 
   function function3 () {... } 
}

Now the "Add Empty Row" button calls its own function "addEmptyRow".
But in this function I have to:
- do an other require for each dojo-module again
- I CAN'T use any of the functions which are "inside" of the "fillReportTable"-function. For example the "createNewRow"-function

 function addEmptyRow() {
require(["dojo/dom-construct", "dojo/dom-attr", "dijit/form/FilteringSelect",
"dojo/data/ItemFileReadStore", "dijit/form/ComboBox", "dijit/form/DateTextBox", "dijit/form/Select", "dojo/store/Memory"],
     function (domConstruct, domAttr, FilteringSelect, ItemFileReadStore, ComboBox, DateTextBox, Select, Memory) {
// a lot of code to create the table, consisting of SEVERAL functions
}

This all seems to be so much complicated with AMD.
Or am i missing something obvious here ?
With AMD if you separate your code into a lot of small functions, do you do the "require" inside EACH function all over again ? Or do you put all the functions inside one "require" with the full list ?
If you do it the second way, how can you call these functions from widget events?

3 Answers 3

11

The easiest way would be to define your own module. Take a look at this tutorial first:

http://dojotoolkit.org/documentation/tutorials/1.7/modules/

Now define your own module, e.g. "./js/mymodules/mymodule.js" (relative to HTML page):

define([
    "dojo/dom-construct",
    "dojo/dom-attr",
    "dijit/form/FilteringSelect",
    "dojo/data/ItemFileReadStore",
    "dijit/form/ComboBox",
    "dijit/form/DateTextBox",
    "dijit/form/Select",
    "dojo/store/Memory"
], function (domConstruct, domAttr, FilteringSelect, ItemFileReadStore, ComboBox, DateTextBox, Select, Memory) {

    function fillReportTable(repId) {
       // a lot of code to create the table, consisting of SEVERAL functions 
       function createNewRow(tbl) { ...} 
       function function1 () {... } 
       function function2 () {... } 
       function function3 () {... } 
    }

    function addEmptyRow() {
       // a lot of code to create the table, consisting of SEVERAL functions
    }

    // Return an object that exposes two functions
    return {
        fillReportTable: fillReportTable,
        addEmptyRow: addEmptyRow
    }

});

And use your module like this:

<html>

<head>

<script>
var dojoConfig = {
    baseUrl: "./js/",
    packages: [
        { name: "dojo", location: "lib/dojo" },
        { name: "dijit", location: "lib/dijit" },
        { name: "dojox", location: "lib/dojox" }
    ]
};
</script>

<script data-dojo-config="async: true" src="js/lib/dojo/dojo.js"></script>

</head>

...

<script>
require([
    "mymodules/mymodule"
], function (mymodule) {
    mymodule.fillReportTable(...);
    mymodule.addEmptyRow(...);
});
</script>
4
  • so, fillReportTable: fillReportTable in the return object will expose fillReportTable() function? Mar 9, 2012 at 15:32
  • Yes that's right. When the mymodule module is required, the AMD loader will load a particular JS file for that module (in our case 'mymodules/mymodule.js'. Within that JS file you pass a function to define, and this function is used to 'export' the module's functionality. In your case, the module represents an object that has two helper functions.
    – Paul Grime
    Mar 9, 2012 at 16:28
  • So now I can call mymodule.fillReportTable() from anywhere outside of the require?
    – Andy N
    Apr 5, 2012 at 20:02
  • In the above require set the objects dojo.setObject('fillReportTable', mymodule.fillReportTable). Then outside of the require do a getObject. var fillReportTable = dojo.getObject('fillReportTable'); Now fillReportTable() is in global space.
    – Andy N
    Apr 5, 2012 at 20:48
4

Try this:

require([...], function() {
    var myFunctions = dojo.getObject('myFunctions', true);
    myFunctions.createNewRow = function(...) {
        ...
    };
});

You can now call your functions from anywhere by using

myFunctions.createNewRow();

If you don't want 'myFunctions', you could do

require([...], function() {
    var createNewRow = function(...) {};

    dojo.setObject('createNewRow', createNewRow);
});
0
3

Paul Grime gave you a good example, so I'm just sharing some thoughts.

You don't define all the modules in each function, that's a huge waste of space. Although, even if you try to load a module multiple times, Dojo will only load it once anyway.

This is a simplified module from my latest project, with quite meaningless functionality:

//app module in 'my' folder

define(
[
    'app/elements',
    'dojo/query',
    'dojo/on',
    'dojo/fx',
    'dojo/_base/fx',
    'dojo/dom-construct',
    'dojo/_base/event'

    //and so on.....
], 

function(elements, q, on, fx, baseFx, constr, event)
{   
    return {

        init : function()
        {
            var node = q(elements.loading);
            this.removeNode(node);

            this.addEvents();
        },

        removeNode : function(node)
        {
            node.remove();
        },

        addEvents : function()
        {
            $(elements.buttons).on('click', function(e)
            {
                alert(q(e).attr('id') + ' was clicked!');
            });
        }   
    }
}

Then I get the module by using

define(
[
    'my/app',
], 

function (app) 
{
    app.init();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.