How exactly are you instantiating it? I have this working fine in a test file. My guess is you may not be taking into account Dojo's asynchronous nature. dojo.require() is an async call, it invokes xhr under normal conditions. Because of that nature, any code that is going to use dojo apis should be put into a function passed to the dojo.addOnLoad() call. Here's the example I have that works fine for me:
<html>
<head>
<title>Test of data loading ItemFileReadStore</title>
<style type="text/css">
@import "dijit/themes/tundra/tundra.css";
@import "dojo/resources/dojo.css";
@import "dijit/tests/css/dijitTests.css";
</style>
<script type="text/javascript" src="dojo/dojo.js" djConfig="isDebug: true, parseOnLoad: true, usePlainJson: true"></script>
<script type="text/javascript">
dojo.require("dojo.data.ItemFileReadStore");
function init(){
var imgCtx = {data: {identifier:'item_id', items:[{item_id:1,title:'title',caption:'cap',alt_text:'alt',credits:'cred',height:'100',width:'200'}]}};
var db = new dojo.data.ItemFileReadStore(imgCtx);
function gotItems1(items, request){
document.body.appendChild(document.createTextNode("Adding in the values for all items for store named 'db':"));
document.body.appendChild(document.createElement("br"));
for(var i = 0; i < items.length; i++){
document.body.appendChild(document.createTextNode(db.getValue(items[i], "title")));
document.body.appendChild(document.createElement("br"));
document.body.appendChild(document.createTextNode(db.getValue(items[i], "width")));
document.body.appendChild(document.createElement("br"));
document.body.appendChild(document.createTextNode(db.getValue(items[i], "height")));
}
document.body.appendChild(document.createElement("br"));
}
function onError(error, request){
alert(error);
console.debug(error);
}
var request = db.fetch({query:{}, onComplete: gotItems1, onError: onError});
var data = {data: {
identifier : 'id',
items : [
{ id : 'a', label: "Letter A" },
{ id : 'b', label: "Letter B" },
{ id : 'c', label: "Letter C" },
{ id : 'd', label: "Letter D" }
]
}};
var store2 = new dojo.data.ItemFileReadStore(data);
function gotItems2(items, request){
document.body.appendChild(document.createTextNode("Adding in the values for all items for store named 'store2':"));
document.body.appendChild(document.createElement("br"));
for(var i = 0; i < items.length; i++){
document.body.appendChild(document.createTextNode(store2.getValue(items[i], "id")));
document.body.appendChild(document.createElement("br"));
document.body.appendChild(document.createTextNode(store2.getValue(items[i], "label")));
document.body.appendChild(document.createElement("br"));
}
document.body.appendChild(document.createElement("br"));
}
var request2 = store2.fetch({query:{}, onComplete: gotItems2, onError: onError});
}
dojo.addOnLoad(init);
</script>
</head>
<body class="tundra">
</body>
End result in my browser is as followS:
Adding in the values for all items for store named 'db':
title
200
100
Adding in the values for all items for store named 'store2':
a
Letter A
b
Letter B
c
Letter C
d
Letter D
pcarmichael wrote:
This is exactly what I was wanting to do too :-)
I just tried it with Dojo 1.0.2, and it doesn't work for me.
I might be missing something in the rest of the code.
Would you mind posting the complete code, so I try your version?
>
> var data = {data: {
> identifier : 'id',
> items : [
> { id : 'a', label: "Letter A" },
> { id : 'b', label: "Letter B" },
> { id : 'c', label: "Letter C" },
> { id : 'd', label: "Letter D" }
> ]
> };
>
> var myNewStore=new dojo.data.ItemFileReadStore(data);
>
> See how that handles it.