curt
curt

Reputation: 4592

Dojo Function Parameter Order

When declaring a function using dojo, there appears to be a proper order for the parameters. This function header caused an error:

require(["dojo/dom",
    "dijit/Dialog",
    "dijit/form/Form",
    "dijit/form/TextBox",
    "dijit/form/ValidationTextBox",
    "dijit/form/Textarea",
    "dijit/form/Button",
    "dojox/validate/web",
    "dojo/request",
    "dojo/domReady!"],
function(dom, Dialog, Form, TextBox, ValidationTextBox, TextArea, Button, request)

The execution of a request.post resulted in an undefined error. I then changed the order of the parks to this:

require(["dojo/dom",
    "dojo/request",
    "dijit/Dialog",
    "dijit/form/Form",
    "dijit/form/TextBox",
    "dijit/form/ValidationTextBox",
    "dijit/form/Textarea",
    "dijit/form/Button",
    "dojox/validate/web",
    "dojo/domReady!"],
function(dom, request, Dialog, Form, TextBox, ValidationTextBox, TextArea, Button ){

By moving dojo/request and request earlier in the order, the error went away. I had the same problem in another script with different parms. Is there some documentation that explains how to order the parameters? Do dojo elements have to precede digit elements, dojo/domReady not withstanding?

Upvotes: 1

Views: 239

Answers (3)

flux9998
flux9998

Reputation: 350

when working with dojo, i think of the require([""]) portion as parameter type for the parameters being passed into your function.

therefore,

require(["dojo/dom",
"dijit/Dialog",
"dijit/form/Form",
"dijit/form/TextBox",
"dijit/form/ValidationTextBox",
"dijit/form/Textarea",
"dijit/form/Button",
"dojox/validate/web",
"dojo/request",
"dojo/domReady!"],
function(dom, Dialog, Form, TextBox, ValidationTextBox, TextArea, Button, request)

when you try to call

request.post

it's handling the 'request' parameter as if it is a type of 'dojox/validate/web' object. if your function signature was:

function(dom, Dialog, form, TextBox, ValidationTextBox, TextArea, Button, validateWeb, request)

your code would have worked fine.

another analogy of sorts is to think of it along the lines of:

function("dojo/dom" dom, "dijit/Dialog" Dialog, "dijit/form/Form" Form, ..."dojo/request" request, string[] args0)

the above is obviously improper syntax, but it helps me to visualize it in that way as though it's a language such as java or .net where when writing a function, you declare the parameter types followed by the name of the parameter.

Upvotes: 0

Royston Shufflebotham
Royston Shufflebotham

Reputation: 970

The order matters, as Dimitri says, but it's really the Asynchronous Module Definition (AMD) specification that describes this: it's not a Dojo-specific thing.

If you use another AMD loader, such as requirejs, you'll see the same behaviour.

Upvotes: 1

Dimitri Mestdagh
Dimitri Mestdagh

Reputation: 44725

The order should be the same as the order you maintain in the array of module names. This means that the following is correct:

require(["my/first", "my/second"], function(first, second) {
    ...
});

However, when you're working with modules that don't need a return value in the callback, they should be inserted last.

require(["my/first", "my/second", "dojo/domReady!"], function(first, second) {

});

If you don't maintain that order, for example in the next example, then you will get problems because the parameter will contain something that doesn't work.

require(["my/first", "dojo/domReady!", "my/second"], function(first, second) {
    // "second" doesn't work
});

In your first example you're importing dojox/validate/web before dojo/request but in your callback you have no parameter for dojox/validate/web. The result is simple, the parameter with the name request will contain the returned object of dojox/validate/web and not of dojo/request. Because of this, calling the request object will probably not work.

So what you need to remember is that you need to maintain the proper order AND that modules that have no return value (or a return value you don't need) should be placed last.

If you don't place them at the last position, then you have to specify a return value, for example, the next example will work:

require(["my/first", "dojo/domReady!", "my/second"], function(first, domReady, second) {
    ...
});

But this is a waste of memory since the domReady parameter will not contain any useful data (at least, if you don't use it).

Upvotes: 4

Related Questions