PDA

View Full Version : similar save related method like isLoading() is to load ?



dedoz
12 Mar 2012, 11:21 PM
after a store.load() (besides the callback) theres a method isLoading() to know if the store is still loading.

is there a similar to store.sync() ? a method to know if the store is still doing a sync ?
it is hasPendingRequests() ? the docs for hasPendingRequests just says "return the number of pending requests" D:

mitchellsimoens
13 Mar 2012, 10:41 AM
Unfortunately not built-in

dedoz
13 Mar 2012, 12:38 PM
D: ok thanks.
only way i could implement this is using AbstractStore.sync mechanism to detect if he needs to do a sync.
used this after doing a store.sync() "if that store doesnt need to be sync then the sync is ended"
which traslates to (example)


function storeStillSync (s) {
// abstractStore.sync uses something similar to dectect if the store needs a sync.
return s.getNewRecords().length +
s.getUpdatedRecords().length +
s.getRemovedRecords().length > 0 ;
}

store1.sync({callback:syncReady});
store2.sync({callback:syncReady});
store3.sync({callback:syncReady});

function syncsReady ()
{ if ( storeStillSync(store1) || storeStillSync(store2) || storeStillSync(store3)) return ;
.... code to execute after all stores finish their sync ....
}

vietits
13 Mar 2012, 5:01 PM
FYI: This is my implementation.


Ext.define('Its.data.Store', {
extend: 'Ext.data.Store',

constructor: function(config) {
var me = this;
me.callParent(arguments);
me.counters = {
create : 0,
update : 0,
destroy: 0
};
me.on({
scope: me,
beforesync: function(options){
Ext.each(['create','update','destroy'], function(action){
if(Ext.isDefined(options[action])){
me.counters[action]++;
}
}, me);
return true;
},
write: function(store, operation){
me.counters[operation.action]--;
}
});
},
isCreating: function(){
return this.counters.create > 0;
},
isUpdating: function(){
return this.counters.update > 0;
},
isRemoving: function(){
return this.counters.destroy > 0;
},
isWriting: function(){
var me = this;
return me.isCreating() || me.isUpdating() || me.isRemoving();
},
...

dedoz
13 Mar 2012, 6:16 PM
yeah thats very similar


isWriting: function(){
var me = this;
return me.isCreating() || me.isUpdating() || me.isRemoving();
},

to


return s.getNewRecords().length +
s.getUpdatedRecords().length +
s.getRemovedRecords().length > 0 ;


only one lil problem with both implementation is after checking the first inspection (create/newRecords) and then goin to inspect the second one (update) in that moment create/newRecords could have changed, since store is working on a parallel process, but i hope that never happen ;D works for now i guess

vietits
14 Mar 2012, 3:19 PM
I don't think they are similar. Let me take an example: Suppose you config your store with autoSync set to false. Then you add new records or modify or delete existing records. Then <store>.getNewRecords().length + <store>.getUpdatedRecords().length + <store>.getRemovedRecords().length will be greater than 0 but it does not mean the store is synchronizing with the server.

dedoz
14 Mar 2012, 7:10 PM
you are right, but i use that function only after a store.sync() so while using it that way is almost similar, after the sync all those lenght will go to 0.