views:

1734

answers:

3

Hi,

When I use an import such as

<script type="text/javascript" src="http://o.aolcdn.com/dojo/1.2.3/dojo/dojo.xd.js"
      djConfig="parseOnLoad:true, isDebug: true"></script>

I get the error

dojox.data.CsvStore is not a constructor

for lines such as

var stateStore = new dojox.data.CsvStore({url: "dojo-passcsv.php", label: "name"});

but the error vanishes if I use an import from a local installation of dojo such as

<script type="text/javascript" src="dojo-release-1.2.3/dojo/dojo.js"
     djConfig="parseOnLoad:true, isDebug: true"></script>

I would really want to be able to use a CDN hosted dojo installation. Is there a known problem between the DojoX libraries and dojo.xd.js?

Thanks in advance,

Animesh

P.S. The dojo.require("dojox.data.CsvStore"); declarations are in place.

P.P.S The full "working code" is below. Replacing the CSS and JS references with those from the CDN breaks it.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <style type="text/css">
        @import "dojo-release-1.2.3/dijit/themes/tundra/tundra.css";
        @import "dojo-release-1.2.3/dojo/resources/dojo.css"
    </style>
    <script type="text/javascript" src="dojo-release-1.2.3/dojo/dojo.js"
         djConfig="parseOnLoad:true, isDebug: true"></script>
    <script>
     dojo.require("dojox.data.CsvStore");
     dojo.require("dijit.Tree");
     dojo.require("dojo.parser");
    </script>
    <script type="text/javascript">
     var stateStore = new dojox.data.CsvStore({url: "states.csv", label: "name"});
    </script>


</head>
<body class="tundra">
    <div dojoType="dijit.Tree" store="stateStore" labelAttr="name" label="States">
    </div>
</body>
</html>
+1  A: 

Are you running that code inside dojo.addOnLoad()? As in:

dojo.addOnLoad(function(){
   dojo.require("dojox.data.CsvStore");
   var stateStore = new dojox.data.CsvStore({url: "dojo-passcsv.php", label: "name"});
});

Also, are you using FireFox 3? If so, try putting your <script></script> block at the very end of the <body> section, just before the closing </body> tag. (I know that is not standard practice, but it's related to Firefox's bug 444322, which should be fixed in release 3.0.6.)

Other than that, your code seems to be fine, and such strange discrepancies usually boil down to issues of timing with the loading of dojo modules.

pierdeux
Thanks for your answer. I have pasted the full code that "works" in the question now. I am not using the var stateStore declaration within dojo.addOnLoad(), but it seems to work all right. However, the moment I put in CDN references, it breaks.
Animesh
Your problem: you're tying to instantiate a class that is not loaded yet.It works when you're loading your files locally because they're loaded before your javascript is executed. (loading files locally is faster than using a CDN, for sure).Anyway, using dojo.addOnLoad *is the only way to go* with XD loading.
Brian Clozel
A: 

Reacting to your update:

I strongly think you should try with dojo.addOnLoad(). Together, the last two <script> sections of your <head> would become:

<script>
   dojo.addOnLoad(function(){
      dojo.require("dojox.data.CsvStore");
      dojo.require("dijit.Tree");
      dojo.require("dojo.parser");  /* I don't think you really need this line */
      var stateStore = new dojox.data.CsvStore({url: "states.csv", label: "name"});
   });
</script>

The problem with your original code is that you cannot guarantee that the constructor function dojox.data.CsvStore has been read by the time you are about to create your stateStore instance of it. That's where dojo.addOnLoad() comes in, giving you a guarantee that the rest of the javascript was loaded prior to executing the abstract function passed as parameter of addOnLoad().

Because it's an issue of timing, your own original code may work sometimes, maybe not others: it would depend on the download speed and the order in which your browser pieces together the various javascript bits. That's why using dojo's remote library may occasionally give different results from using your own local copy of the dojo library.

That said, if you are using Firefox 3 (earlier than 3.0.6), then bear in mind what I said about the known bug. In that case, you may want to put that <script> block immediately before the closing </body> tag... (That option would work on other browsers as well.)

pierdeux
A: 

Pierdeux was correct on that that addOnLoad is the key, but it should have been after dojo.requires, not before. In addition, one has to switch from automatic djConfig.parseOnLoad (it fires before addOnLoad) to manually initiating parser. When you still change that store URL to point to some sensible location (on your site), this works:

<script>
   dojo.require("dojox.data.CsvStore");
   dojo.require("dijit.Tree");
   dojo.require("dojo.parser");
   dojo.addOnLoad(function(){
      stateStore = new dojox.data.CsvStore({url: "states.csv", label: "name"});
      dojo.parser.parse();
   });
</script>

Note: there's another very similar case: Grid, stores, XD.

Maine