Login Register

DataStore and FilteringSelect

My problem is this: I'm having difficulty taking the JSON data I receive from a Servlet after transforming it from JSON text to a JSON Object and using it as a dojo.data.ItemFileReadStore data store in a dijit.form.FilteringSelect. Please see below for a detailed description of the problem.

Summary: Presently I'm working on a UI widget that will dynamically populate a dijit.form.FilteringSelect widget with data that is supplied from a database returned in an HTTP response object using the dojo.xhrGet() method. After struggling some to understand how things should be working I've successfully built up a JSON object in Java and returned it from the servlet in the HTTP response object.

The JSON text (a list of publications) looks like this coming from the servlet:

[
  {"code":"01",
   "runCodes":[{"name":"Mainsheet","codes":["MA"]}],
   "name":"Publication1",
   "id":1},
  {"code":"02",
   "runCodes":[{"name":"Mainsheet","codes":["MA"]}],
   "name":"Publication2",
   "id":2},
  {"code":"03","runCodes":[{"name":"Mainsheet","codes":["MA"]}],
   "name":"Publication3",
   "id":3},
  {"code":"04","runCodes":[{"name":"Mainsheet","codes":["MA"]}],
   "name":"Publication4",
   "id":4},
   ...
  {"code":"99","runCodes":[{"name":"Mainsheet","codes":["MA"]}],
   "name":"Publication99",
   "id":99}
]

This seems to be in line with an acceptable format of data for parsing JSON as I transform the JSON text that comes from the servlet into an actual JSON object using JavaScript's eval() method just after I perform the xhrGet method (which, by the way is a synchronous, or blocking, request). The following code is a snippet of how I'm doing this:

var xhr = { sync: true,
            url: 'path/to/Servlet',
            handleAs: 'text',
            content: {action: 'xhr'},
           
            load: function(response, args) {
              console.debug("Loading the XHR...");
              return(response);
            },

            error: function(response, args) {
            console.error("XML HTTP Request Error with [HTTP] status code: ", args.xhr.status);
            return(response);
            }};
             
var response = dojo.xhrGet(xhr);
this.publications = eval('(' + response.results[0] + ')');

My problem lies in using this JSON object to populate a data store to be used with a filtering select, the code I use to create the filtering select is as follows:

var jsonObj = {identifier: 'code', label: 'name', items: this.publications};
var dataStore = new dojo.data.ItemFileReadStore({data: jsonObj});
var fs = new dijit.form.FilteringSelect({name: 'publication', store: dataStore}, this.itemNode);
fs.startup();

But when the code attempts to execute the code to create the new FilteringSelect widget, I receive an error "arrayOfValues has no properties." I've narrowed it down and run a few test cases, the error seems to be because when creating the data store, all values are indexed within arrays, even if they are only one element--that being the case the system can't extract the proper value for identifier, and conceivably the label fields.

Is there any way around this, do I need to implement a new data store as an extension of the ItemFileReadStore class? I found some posts in the Dojo forums that might help me, but none that are specific to this issue so far as I could tell.

Reference posts:

Partial Resolution...?

Apparently part of my problem was that the JSON data needs to be a flat structure (without embedded arrays) or else the data store(s) have a problem processing the data in regards to "identifier" and "label" properties. This seems to be the resolution to the problem I was having.