ExtJS - data storage in several stores

I have three stores ( Ext.data.JsonStore ) inside a subclass of Ext.Window . I would like to call save() on all of these, show a wait mask before completion, and then close the window or handle the error.

Is there a way I can do this without ending up with something really dirty? I considered doing this by calling save() in the second store from the save event on the first, etc., and then putting something together so that I could set up a callback that will be called when the last store finishes saving, but it looks like i will have a big nested mess. Therefore, any suggestions on how to do this will be greatly appreciated.

+4
source share
2 answers

Sort of:

 var saveStores = function(stores) { if (stores.length === 0) { console.log("All stores saved"); } else { var store = stores.pop(); store.save({ callback : function() { saveStores(stores); } }); } } saveStores([store1, store2, store3]); 

Sorry, there is no time for testing - but algo may be there.

+1
source

In the end, I used something like Drasill, but it was a bit confusing. I guess this will ruin if you called him twice, without waiting for the first time, because of how the events work.

 var save_multiple_stores = function(stores, success, fail) { if (stores.length === 0) { if (success) { success(); } } else { var store = stores.pop(); var listeners = {} var unbind_listeners = function() { for (var evt in listeners) { store.un(evt, listeners[evt]); } } var onsuccess = function() { unbind_listeners(); save_multiple_stores(stores, success, fail); } var onfail = function() { unbind_listeners(); if (fail) { fail(); } } store.on('save', onsuccess); listeners.save = onsuccess; store.on('exception', onfail); listeners.exception = onfail; var num_records = store.save(); //save event will not fire if there are no changed records if (num_records <= 0) { onsuccess(); } } }; 
0
source

All Articles