teaman
teaman

Reputation: 479

Dojo ItemFileWriteStore not reading JSON server file

I am using Dojo and it's dojo/data/ItemFileWriteStore module to read a JSON data file on my local server. In my .js file I have

var myDataStore = new ItemFileWriteStore({
    url: "app/data/mydata.json",
    handleAs: "json",
    clearOnClose: true,
    urlPreventCache: true
})

This is located in the postCreate function for my return declare function... so:

 define([
    "dojo/_base/declare",
    "com/cayuse/base/_widget",
    "dojo/text!./templates/myform.html",
    ...    
    "dojo/data/ItemFileWriteStore",
    "dojo/store/DataStore",
    "dojo/store/Observable",
    "dojo/data/ObjectStore",
    "dojo/domReady!"
    ],
    function(declare, widget, template, ..., ItemFileWriteStore, DataStore, 
        Observable, ObjectStore){
        return declare("app.myform", widget, {
            templateString: template,

            postCreate: function(){

                domConstruct.create("link",{
                    type: "text/css",
                    rel: "stylesheet",
                    href: require.toUrl('dojox/form/resources/CheckedMultiSelect.css')
                }, document.getElementsByTagName("head")[0]);

                // data store
                var myDataStore = new ItemFileWriteStore({
                    url: "app/data/mydata.json",
                    handleAs: "json",
                    clearOnClose: true,
                    urlPreventCache: true
                })
                console.log(myDataStore);
            }
        });
    }
);

I can change the data store access from what you see above using IFWS method to

var myDataStore = dojo.xhrGet({
    url: "app/data/mydata.json",
    handleAs: "json",
    load: function(data, ioArgs){
         console.log(data);
    }
 });

and it finds the file with no problems.

This is so bizarre! Any ideas on what is going wrong here?

UPDATED: Here is the data in the file I am reading. I believe it conforms to the JSON format. Let me know if not. xhrGet reads it fine.

{ "identifier": "id",
    "label": "firstName",
    "items":[
     {"id":"0","firstName":"Robert","website":"www.barker.com","email":"[email protected]","bday":"1928-08-09","color":"Blue","toolkits":["Dojo","Moo"],"sendEmail":["on"],"format":"HTML"},
     {"id":"1","firstName":"Vanna","website":"www.white.com","email":"[email protected]","bday":"1968-07-23","color":"Green","toolkits":["Dojo","jQuery"],"sendEmail":["off"],"format":"Text"}
    ]
}

Upvotes: 1

Views: 3950

Answers (3)

Don
Don

Reputation: 1

If your code is EXACTLY as you posted it above then the interpreter might not like the fact that you omitted the semicolon from the ItemFileWriteStore assignment. Try adding the ';' as below:

            // data store
            var myDataStore = new ItemFileWriteStore({
                url: "app/data/mydata.json",
                handleAs: "json",
                clearOnClose: true,
                urlPreventCache: true
            });
            console.log(myDataStore);

Upvotes: 0

Magdy
Magdy

Reputation: 1

Try using dojo.data.ItemFileReadStore for reading json data files, instead of dojo/data/ItemFileWriteStore.

Note that dojo.data.ItemFileWriteStore is used for writting json data.

Upvotes: 0

eburgos
eburgos

Reputation: 2053

ItemFileWriteStore requires your data being structured into something like this:

{ identifier: 'abbr',
  label: 'name',
  items: [
    { abbr:'ec', name:'Ecuador',           capital:'Quito' },
    { abbr:'eg', name:'Egypt',             capital:'Cairo' },
    { abbr:'sv', name:'El Salvador',       capital:'San Salvador' },
    { abbr:'gq', name:'Equatorial Guinea', capital:'Malabo' },
    { abbr:'er', name:'Eritrea',           capital:'Asmara' },
    { abbr:'ee', name:'Estonia',           capital:'Tallinn' },
    { abbr:'et', name:'Ethiopia',          capital:'Addis Ababa' }
]}

That is 'identifier' being your "ID" field, 'label' being your "label" field and then all your objects inside an array called "items".

You can check it out here in ItemFileWriteStore's documentation. If you don't have your JSON data structured like that it's possible that you may end up reading your file with the IFWS and actually not reading any data.

There are other store implementations in dojo 1.7 that don't require such structure, e.g. Memory Store that you can combine with other file reading techniques to achieve the same.

Upvotes: 2

Related Questions