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