I’m having an issue where a nested array associated inside an object causes a fetch declaration to fail. The actual JSON itself is valid, and when the array itself is commented, it works fine. I’d like to parse the entire object, and eventually access the array.
Here’s the object (the nefarious array is inside the “Page 2” item):
{
"identifier":"pageTitle",
"label":"pageTitle",
"items":[
{
"pageTitle":"Page 1",
"pageUrl":"#"
},
{
"pageTitle":"Page 2",
"subTopics":[
{
"subTitle":"Subpage 1"
}
],
"pageUrl":"index.html"
},
{
"pageTitle":"Page 3",
"pageUrl":"#"
}
]
}
and the fetch code (fairly simple)
try {
var JSONObject = new dojo.data.ItemFileReadStore({url: "object.json"});
//Fetch nav objects and start building
JSONObject.fetch({
query: {"pageTitle":"*"},
onBegin: clearNav,
onComplete: buildNav,
onError: failureNav
});
} catch(xcept) {
console.warn("Failed to create JSONObject", xcept);
}
And related functions
function clearNav(items, req) {
console.log("clearNav()");
dojo.query("nav.navigation ul").forEach(function(items, idx) {
items.innerHTML="";
});
console.warn("Navigation destroyed...");
}
function buildNav(items, req) {
console.log("buildNav()");
console.debug("items", items);
var theList = dojo.query("nav.navigation ul")[0];
dojo.forEach(items, function(item, idx) {
var newLI = document.createElement("li");
console.warn("url", document.URL);
if (document.URL.search(item.pageUrl)) {
newLI.setAttribute("class","ac");
}
newLI.innerHTML = "<a href=\""+item.pageUrl+"\" >"+item.pageTitle+"</a>";
theList.appendChild(newLI);
});
}
function failureNav(e) {
console.debug("Error", e);
}
You have two choices:
either format your JSON nested object (“subTopicts”) to conform to having the same identifier name of the main one;
or let Dojo ignore that nested object, creating the ItemFileReadStore with parameter herarchical : “false”
You find this documented in the doc.