Advertisement
libdo

Untitled

Sep 20th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var store1Loaded = false;
  2. var store2Loaded = false;
  3.  
  4. store1.on('load', function(){
  5. store1Loaded = true;
  6. });
  7.  
  8. store2.on('load', function(){
  9. store1Loaded = true;
  10. });
  11.  
  12.  
  13. store1.load();
  14. store2.load();
  15.  
  16. function WaitForFunction()
  17. {
  18. if (!store1Loaded || !store2Loaded)) {
  19. setTimeout( WaitForFunction, 100);
  20. return;
  21. }
  22. AllStoresLoaded();
  23. }
  24.  
  25. function AllStoresLoaded(){
  26. //Do Something
  27. }
  28.  
  29. var store1 = Ext.create('Ext.data.Store', {
  30. model: myModel,
  31. storeId: 'store1', // store needs to be done MVC style or have this config
  32. proxy: {
  33. type: 'ajax',
  34. url: 'url...',
  35. reader: 'json'
  36. },
  37. autoLoad: {
  38. callback: initData // do this function when it loads
  39. }
  40. });
  41.  
  42. var store2 = Ext.create('Ext.data.Store', {
  43. model: myModel,
  44. storeId: 'store2',
  45. proxy: {
  46. type: 'ajax',
  47. url: 'url...',
  48. reader: 'json'
  49. },
  50. autoLoad: {
  51. callback: initData // do this function when it loads
  52. }
  53. });
  54.  
  55. // the stores to be checked
  56. var myComboStores = ['store1', 'store2']
  57.  
  58. // function does logic if they are both finished loading
  59. function initData() {
  60. var loaded = true;
  61. Ext.each(myComboStores, function(storeId) {
  62. var store = Ext.getStore(storeId);
  63. if (store.isLoading()) {
  64. loaded = false;
  65. }
  66. });
  67. if(loaded) {
  68. // do stuff with the data
  69. }
  70. }
  71.  
  72. var allStores = [store1, store2],
  73. len = allStores.length,
  74. loadedStores = 0,
  75. i = 0;
  76.  
  77. for (; i < len; ++i) {
  78. allStores[i].on('load', check, null, {single: true});
  79. }
  80.  
  81. function check() {
  82. if (++loadedStores === len) {
  83. AllStoresLoaded();
  84. }
  85. }
  86.  
  87. function AllStoresLoaded() {
  88. //Do Something
  89. }
  90.  
  91. function storeLoadingHandler(justLoadedStore) {
  92. // I use some quick hacky flag, you can do it by your own way
  93. justLoadedStore.loaded = true;
  94.  
  95. var allStoresLoaded = true;
  96.  
  97. // just walk through all stores registered in the application
  98. // do not forget to use MVC-style stores or add 'storeId' manually
  99. // or register every store with your custom code
  100. Ext.StoreManager.each(function(existingStore) {
  101. // we have to ignore system stores
  102. if (existingStore.storeId != 'ext-empty-store') {
  103. if (!existingStore.loaded) { // our flag or undefined
  104. // nope, at least one of stores is not loaded
  105. allStoresLoaded = false;
  106.  
  107. return false
  108. }
  109. }
  110. })
  111.  
  112. if (allStoresLoaded) // then do something
  113. alert('All stores are loaded.');
  114. }
  115.  
  116. // add the loading handler for all stores
  117. Ext.StoreManager.each(function() {
  118. this.on('load', storeLoadingHandler);
  119. })
  120.  
  121. Ext.create("Ext.data.Store", {
  122. ...,
  123. loading: true,
  124. ...
  125. });
  126.  
  127. function createStore(id) {
  128. return Ext.create('Ext.data.Store', {
  129. storeId: 'store_' + id,
  130. alias: 'store.store_' + id,
  131. proxy: {
  132. type: 'ajax',
  133. url: 'data.json',
  134. timeout: 300000,
  135. reader: {
  136. type: 'json',
  137. rootProperty: 'data'
  138. }
  139. }
  140. });
  141. }
  142.  
  143. var storeArry = [],
  144. store = '';
  145.  
  146. for (var key = 0; key < 5; key++) {
  147. storeArry.push(createStore(key).storeId);
  148. }
  149.  
  150. Ext.getBody().mask('Please wait..');
  151. Ext.defer(function () {
  152. Ext.getBody().unmask();
  153. Ext.Array.forEach(storeArry, function (storeId) {
  154. //For checking store is created or not
  155. //if store is not created we can create dyanamically using passing storeId/alias
  156. store = Ext.getStore(storeId);
  157. if (Ext.isDefined(store) == false) {
  158. store = Ext.create(storeId);
  159. }
  160. store.load({
  161. callback: function () {
  162. //On every store call back we can remove data from storeArray or maintain a veribale for checking.
  163. Ext.Array.remove(storeArry, this.storeId);
  164. if (storeArry.length == 0) {
  165. Ext.Msg.alert('Success', 'All stored is loaded..!');
  166. }
  167. }
  168. });
  169. });
  170. }, 3000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement