Sridhar
Sridhar

Reputation: 93

DOJO reference error: declare is not defined

I was following the jsfiddle link http://jsfiddle.net/phusick/894af and when I put the same code into my application, I was getting "reference error: declare is not defined". I have following declarations on top of my js file:

dojo.require("dojo._base.declare");
dojo.require("dojox.form.CheckedMultiSelect");

Thanks in advance for your help.

Upvotes: 0

Views: 1046

Answers (1)

Dimitri Mestdagh
Dimitri Mestdagh

Reputation: 44745

With Dojo AMD you can tell which module maps to which parameter, for example dojo/_base/declare which is mapped to a variable called declare.

However, in non-AMD code you don't have this possibility. In stead of that you have to do the following:

dojo.require('dojo._base.declare'); // Import
dojo.declare(/** Parameters */); // Use

And actually, modules in dojo/_base are already inside the Dojo core if I'm not mistaken, so you could leave away the dojo.require() line in this case.

For the following AMD code:

require(["dojo/_base/declare"], function(declare) {
  var MyCheckedMultiSelect = declare(CheckedMultiSelect, {
    /** Stuff */
  });
});

You can write the following in non-AMD:

var MyCheckedMultiSelect = dojo.declare(CheckedMultiSelect, {
  /** Stuff */
});

However, make sure that when you're running Dojo 1.7, that you disable async mode, for example:

<script>
  dojoConfig = {
    parseOnLoad: false,
    async: true
  };
</script>

This rule applies to most, if not all, modules in dojo/_base and several DOM modules, for example:

  • dojo/_base/xhr: Methods like put(), get(), ... become dojo.xhrGet(), dojo.xhrPut(), ...
  • dojo/_base/lang: Methods like mixin(), hitch(), ... become dojo.mixin(), dojo.hitch(), ...
  • dojo/dom: Methods like byId() become dojo.byId()
  • dojo/on: You have to use dojo.connect() for this
  • dijit/registry: Methods like byId() become dijit.byId()
  • ...

However, if you're using Dojo 1.7, then you should probably just leave the code in AMD even if all other code is written in non-AMD code. Eventually you will have to upgrade all your code to AMD-syntax, if you're now investing time to convert the code to non-AMD and you later have to convert it to AMD again, you're doing the same work twice.

Upvotes: 1

Related Questions