The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

theartfuldodger (23) [Avatar] Offline
#1
I am reading a JSON file from localStorage and attempting to load it into a DataStore and display it in a List. Nothing is being displayed. Same code works fine when I substitute a url for the proxy.

Any ideas?

Here's the code (works fine for loading from a url):

jsonStr = localStorage.getItem("Cases");

jsonObj = Ext.util.JSON.decode(jsonStr);

var testStore = new Ext.data.Store({
proxy: new Ext.data.MemoryProxy(jsonObj),
reader: new Ext.data.JsonReader({
root: 'Cases',
},
['Case', 'Name', 'Plaintiff', 'Defendant', 'Client', 'Adverse', 'CaseNo', 'Dept', 'Docket']
)
});
testStore.load();
jesus.garcia (463) [Avatar] Offline
#2
Re: Unable to Load Local JSON Data into Store
is jsonObj actually an Object?

Btw, we have a meetup close to you smilie http://www.meetup.com/NoVa-Javascript-Ext-JS-Users-Group/calendar/15201679/
theartfuldodger (23) [Avatar] Offline
#3
Re: Unable to Load Local JSON Data into Store
Got it to work using eval. Yes, I know they're "evil", but damned useful in this case.

e.g. jsonObj = eval("(" + Ext.util.JSON.decode(jsonStr) + ")");

However, I also want to be able to write the store to the server for a backup (and read it back again for a restore).

I see there is an "each" method that would allow me to write each record out, but I would ideally like to grab the entire store object (or a stringified version of it) and write/read that.

Is this possible? Do I need a PHP program?
jesus.garcia (463) [Avatar] Offline
#4
Re: Unable to Load Local JSON Data into Store
Eval is not just evil, it's slow.

That said, decode should take care of that for you. What does jsonStr look like?

You can use :

var data = [];

Ext.each(store.getRange(), function(rec) {
data.push(rec.data);
});



// do something with the data here.
theartfuldodger (23) [Avatar] Offline
#5
Re: Unable to Load Local JSON Data into Store
That'll work, thanks.

How can I best write the data object to the server?

This is all in aid of a backup/restore capability. I know how to restore from a server file, but getting it from the client to the server (from ExtJS) is the issue. Do I need a PHP program?