View Full Version : Saving List data to local storage

29 Nov 2010, 2:47 PM
Hello all,

I have looked around the forums for this issue and didn't come across any pertinent posts, but feel free to direct me to a previous thread if I missed one.

I am attempting to take a List backed by a JSON store and save that data to local storage by instantiating another store. I found a post on Ed Spencer's blog talking about local storage in general and tried to apply it to my app, and although the code doesn't throw any errors, it doesn't send the data to local storage either. I am very new to not just Sencha Touch, but also to web application development, so please pardon my likely easily-solvable problem.

I have a model named "Patient" that contains various strings to describe a patient (name, rm_num, sex, dob, etc).

The List object's data is not loaded automatically, but rather on button press which loads the data from an Ajax proxy that receives JSON from a server-side PHP script. Here is the button code:

text: 'Load (Remote)',
ui: 'action',
centered: true,
handler: function() {

This button does work and loads the list appropriately when clicked. Once loaded, I want to "save" this data to local storage. Here is the code for the save button:

text: 'Save',
ui: 'action',
centered: true,
handler: function() {
var localStore = new Ext.data.Store({
proxy: new Ext.data.LocalStorageProxy({
id: 'patients'
model: 'Patient'

When this "save" button is clicked, no error is thrown, but the data is not saved to local storage. Looking in Safari's web inspector, I see a "patients" key that is created (which I assume to come from the id I provided to the localStorageProxy), but there are no other keys or values. In my previous testing I saw how local storage writes additional keys with [id]-1 and then writes the data to the value for that key.

Please let me know if other code or debugging information is needed.

Thanks very much,
Ben Gohlke

15 Dec 2010, 12:33 AM
It's normal. Your localStore is empty.
You just created a new Store that can contain records of model Patient. Before to sync your localStore, you need to fill it with records of your store attached to your list.

15 Dec 2010, 3:01 PM
Try localStore.addAll(patList.getStore().getRange()) before localStore.sync(). This will grab all the Model instances from the first store and add them into the second

17 Dec 2010, 7:05 AM
I got an undefined method when I tried to use addAll(). I think it should be the add() method. However, it doesn't seem to store the 'hasMany' relationship of my model. The model has a parent/child relationship defined and it all works well in the main application.

I have a store that retrieves data and I simply add the returned records to the localStore, but in the Chrome storage inspector, all I see is the parent data. I know the entire parent/child relationship is being returned because the rest of my application (that doesn't rely on local storage yet) works correctly. I cannot retrieve the child data when I do a load() on the localStore. I only get back the parent data.

edit: The data does seem actually exist in the local browser storage. When I clear the cache, and go to about:cache in Chrome, the data seems to be there. Odd that it doesn't show in the Chrome browser inspector.

17 Dec 2010, 11:20 AM
I got an undefined method when I tried to use addAll().

Ah yes - that's a MixedCollection method. Getting my APIs mixed up :)

Store will currently only save the single Model it is configured with - e.g. it won't save associated Model instances. Usually this is what you want when saving to a server side, though perhaps not as useful as it could be if you're trying to persist lots of data locally...

18 Dec 2010, 6:33 AM
... Getting my APIs mixed up :) I know that feeling!

I thought the whole point of the model with the hasMany relationship was to allow the serilization/deserialization of objects. I just assumed that the entire process would just seamlessly work, but now I have to iterate through my multilevel model to do my own serialization?

Hopefully this is on the drawing board for a future release.

19 Dec 2010, 5:36 PM
At the moment it won't save your associated model data. This is probably easier on the client side than the server side... it's on my mind to add something that does this but no firm timeframe as yet