PDA

View Full Version : [CLOSED] LocalStorage / Not Local storage for Store



glenh
May 21, 2015, 7:09 AM
Hi

This is in reference to an old post that I still have not found an elegant solution for...however now I have a theory which may improve what I am currently doing...

Old Post - http://forums.ext.net/showthread.php?45831-CLOSED-Multiple-Stores-design-pattern-required&p=208061#post208061

To sum the old post up - I have a page which can create a "quote" it contains a large number of details with grids and their associated stores - 9 in all. A user may create a new quote and nothing is persisted until you click the save button. The data structure is complex with a series of master / child relationships from store to store and becomes more complicated when you have phantom records referencing other phantom records. I have handcrafted a lot of this to be handled and saving is working but there are occasions where this fails.

In addition to this the field has a load of closable/auto hiding/carosuel etc and lots of javascript. If for whatever reason a javascript bug is hit it is nearly impossible to recover which means losing a lot of data (as the save has not yet occured).

Anyway, my theory now is that I can use localstorage until im ready to save using it as a somewhat backup service.

So my theory is:

For each store:
1. Load data from db to each store
2. Tell the store to use localstorage by changing its proxy to LocalStorageProxy and then make a save on the store to persist to client browser.
3. The user can then edit the screen and the app can persist silently to local storage either perodically with time based polling or systematically (after row updates etc).
4. When the user hits save tell the store to use non-localstorage proxy and commit records

This then means that if it fails I can implement some function to pull the data back from local storage when the user restarts the screen.

I have tried to get this working but I am having a problem changing the localstorageproxy of the store at runtime. I have got a workaround that does sort of work it is a lot more work and not a very nice solution which involves using a duplicate store.

For each store:
1. I setup two stores (1 for local storage and the other normal)
2. On page load I persist data from db into non local storage store (serverside)
3. On Ext.onReady (javascript) I copy data from non localstorage store to localstorage store then call the save button on the localstorage store to persist to client browser.
4. The user edits the quote and perodically the localstorage persists data to the client browser (silently)
5. When user hits save I need to copy data back out from localstoarge store to non localstorage store and then sync to server.

So this works but ideally I would like to only have one store but be able to change proxy from non localstorage to localstorage and back again. This will negate the need for two stores and all the extra work.

Hopefully this makes sense. Really I am just looking for a way to change a store's proxy to localstorage and back again.

Kind Regards
Glen

Vladimir
May 25, 2015, 8:59 AM
Hi,



Really I am just looking for a way to change a store's proxy to localstorage and back again.


Well, to replace proxy of store you just need to set new proxy instance for `proxy` field of store in javascript, like this


App.MyStore1.proxy = new_proxy_instance;


The same approach is used by Ext.Net when we need to retrieve new data from server but current proxy is just memory proxy. Ext.Net store creates new ajax proxy (`createTempProxy` method of Ext.data.AbstractStore (extnet\src\data\Store.js )).

So, replacing proxy for store works really well in ExtJS.

If you post test sample then we can help you with implementation