2012-02-21 16 views
6

लोड करने के लिए प्रतीक्षा कर रहा है मैं एकाधिक स्टोरों को लोड करने की प्रतीक्षा करने के बारे में कैसे जाउंगा? मेरे पास एक ऐसा मामला है जहां मुझे केवल कुछ काम करने की ज़रूरत है जब दो अलग-अलग स्टोर लोड हो जाएं, इसलिए एक स्टोर के store.on("load", fn) का उपयोग करना पर्याप्त नहीं है।ExtJS एकाधिक स्टोरों को

+0

संबंधित करने के लिए http://stackoverflow.com/questions/11003605/how-to-wait-until-all-stores-are-loaded-in-extjs – Patrick

उत्तर

1

मैं तुम दोनों दुकानों के लिए लोड श्रोताओं में जोड़ सकते हैं, और अगर एक दूसरे को समाप्त जाँच लगता है ...

this.getStore('store1').on('load',function(store){ 
    if (this.getStore('store2').isLoading() == false){ 
    // callMethod to perform action .... 
    } 
},this); 

this.getStore('store2').on('load',function(store){ 
    if (this.getStore('store1').isLoading() == false){ 
     // callMethod to perform action.... 
    } 
},this); 

मैं इस तरह से यह विधि केवल जब वे दोनों यह मानते हुए लोड किए गए हैं कॉल करेंगे लगता है कि आप दोनों के लिए लोड के लिए अनुरोध किया गया है पता है।

4

मेरे पास कुछ परियोजनाएं हैं जिनके लिए डेटा के साथ काम करने से पहले कई स्टोर लोड किए जाने की आवश्यकता है। मैंने यह जांचने के लिए उन पर एक कॉलबैक जोड़ा कि Ext.data.StoreManager में प्रत्येक आइटम लोड किया गया था और यदि ऐसा है तो मुझे डेटा के साथ क्या करना होगा।

StoreManager के भंडार को जोड़ने के लिए आप सिर्फ अपने config में यह एक storeId देना है, कुछ इस तरह: क्योंकि मैं क्लाइंट/सर्वर roundtrips कम करने और प्रदर्शन में सुधार करने का प्रयास करते हैं

var store1 = Ext.create('Ext.data.Store', { 
    model: myModel, 
    storeId: 'store1', //<-- adds this to Ext.data.StoreManager 
    proxy: { 
     type: 'ajax', 
     url: 'url...', 
     reader: 'json' 
    }, 
    autoLoad: { 
     callback: initData 
    } 
}); 

var store2 = Ext.create('Ext.data.Store', { 
    model: myModel, 
    storeId: 'store2', 
    proxy: { 
     type: 'ajax', 
     url: 'url...', 
     reader: 'json' 
    }, 
    autoLoad: { 
     callback: initData 
    } 
}); 

// Initialize store dependencies when all stores are loaded 
function initData() { 
    var loaded; 
    Ext.data.StoreManager.each(function(store) { 
     loaded = !store.isLoading();  
     return loaded; 
    }); 
    if(loaded) { 
     // do stuff with the data 
    } 
} 
+2

नहीं निश्चित रूप से, लेकिन मुझे लगता है कि डाउनवोट 'loaded =! store.isLoading(); 'जो' लोड किया जाना चाहिए और =! store.isLoading();'। इसे देखें [jsfiddle] (http://jsfiddle.net/JHMzp/1)। किसी भी मामले में, सभी की मदद करने के लिए एक टिप्पणी होनी चाहिए :) – aletzo

1

आमतौर पर मैं इस समस्या से बचने के : मैंने स्टोर पर ऑटोलोड को गलत पर सेट किया है, फिर एक स्पष्ट AJAX अनुरोध करें जो एक ही समय में सभी स्टोरों के लिए डेटा प्राप्त करता है, फिर स्टोर.लोडडाटा() का उपयोग सीधे प्रत्येक स्टोर में सेट करने के लिए करें। नकारात्मकता निश्चित रूप से है कि इसे अधिक कोड और परिणाम को कड़ा युग्मन में आवश्यक है।

8

हम इसका उपयोग जब वहाँ पर प्रतीक्षा करने के लिए कई दुकानों हैं:

var store1 = Ext.create('Ext.data.Store', { 
    storeId: 'Store1', 
    .... (rest of store config) 
}});   

var store2 = Ext.create('Ext.data.Store', { 
    storeId: 'Store2', 
    .... (rest of store config) 
}});   


var coordinatior = Ext.create('Ext.ux.StoreLoadCoordinator', { 
    stores: ['Store1', 'Store2'], 
    listeners: { 
     load: function() { 
      // Do post-load work 
     } 
    } 
});   

यह आप एक ही लोड दे देंगे:

Ext.define('Ext.ux.StoreLoadCoordinator', { 
mixins: { 
    observable: 'Ext.util.Observable' 
}, 
resetStoreLoadStates: function() { 
    this.storeLoadStates = {};    

    Ext.each(this.stores, function(storeId) { 
     this.storeLoadStates[storeId] = false; 
    }, this);  
},  
isLoadingComplete: function() { 
    for (var i=0; i<this.stores.length; i++) { 
     var key = this.stores[i]; 

     if (this.storeLoadStates[key]==false) { 
      return false; 
     } 
    } 

    return true;   
},  
onStoreLoad: function(store, records, successful, eOpts, storeName) { 
    this.storeLoadStates[store.storeId] = true; 

    if (this.isLoadingComplete()==true) { 
     this.fireEvent('load'); 
     this.resetStoreLoadStates(); 
    } 
},  
constructor: function (config) { 
    this.mixins.observable.constructor.call(this, config); 

    this.resetStoreLoadStates(); 

    Ext.each(this.stores, function(storeId) { 
     var store = Ext.StoreManager.lookup(storeId); 

     store.on('load', Ext.bind(this.onStoreLoad, this, [storeId], true)); 
    }, this); 

    this.addEvents(
     'load'    
    ); 
}}); 

इसके इस्तेमाल के लिये प्रासंगिक storeIds की एक सरणी में पारित कई दुकानों के लिए संभाल करने के लिए घटना। कृपया ध्यान दें कि इसके लिए Ext 4.x या बाद की आवश्यकता है।

+0

बढ़िया! धन्यवाद – Black

1

मैं इसे हल करने के लिए चेन लोडिंग स्टोर का उपयोग करता हूं।

विपक्ष: इससे धीमा होना चाहिए।

chainStoreLoad :function (stores, lastCall, idx) 
{ 
    if (idx === stores.length) { 
     if ("function" === typeof lastCall) { 
      lastCall.call(); 
     } 
     return; 
    } 
    stores[idx].load (function (r,o,s) { 
     Jx.chainStoreLoad (stores, lastCall, idx + 1); 
    }); 
} 

उदाहरण:

Jx.chainStoreLoad (
    [ 
     this.storeAssetType 
    , this.storeAssetProcurement 
    , this.storeAssetStatus 
    , this.storeAssetLocation 
    , this.storeSystemUser 
    ] 
, function() 
    { 
     self.panel.doRefresh (perm); 
    } 
, 0);