PDA

View Full Version : How to check datastore is loaded?



ironlion
11 Feb 2010, 8:45 PM
How to check data store is loaded?
Is there any direct method something like below?
myStore.isLoaded()

evant
11 Feb 2010, 9:00 PM
Use the load event:



store.on('load', function(){
//do something
});

ironlion
11 Feb 2010, 9:10 PM
Load event fires after records has been loaded. I am asking the way to check data store loaded or not.

evant
11 Feb 2010, 9:15 PM
That's how you check, once the load event fires.

ironlion
11 Feb 2010, 9:31 PM
That's how you check, once the load event fires.

Let's assume myStore is not loaded yet. Then how to check it is not loaded?

MaximGB
11 Feb 2010, 9:36 PM
Extend the store - add the 'loaded' property to the child class, override 'initComponent()' and attach to 'load' event inside it, raise the 'loaded' to true inside the event handler, add the 'isLoaded()' method returning value of the 'loaded' property.

Other way, if you use http transport - store.proxy.conn.isLoading()

ironlion
11 Feb 2010, 9:46 PM
Thanks MaximGB, that's good idea.

Eugen_
11 Feb 2010, 9:50 PM
Let's assume myStore is not loaded yet. Then how to check it is not loaded?
if a loaded store has at least one record always you can use the getCount() method

realjax
12 Feb 2010, 12:08 AM
if a loaded store has at least one record always you can use the getCount() method

I'd strongly advise against this. Its dangerous because this just gets you the number of records. It doesn't tell you if that number is after or before loading. Before initial loading getCount returns zero, but after loading and getting back zero records you get the same result..

Eugen_
12 Feb 2010, 12:44 AM
I'd strongly advise against this. Its dangerous because this just gets you the number of records. It doesn't tell you if that number is after or before loading. Before initial loading getCount returns zero, but after loading and getting back zero records you get the same result..

There is no difference between isLoaded() and if (getCount() > 0) if a store contains at least one record always after loading. Before loading we can clear the store. In over case we need to catch 'load' event.

realjax
12 Feb 2010, 6:00 AM
There is no difference between isLoaded() and if (getCount() > 0) if a store contains at least one record always after loading.

Of course there is a difference. (getCount() > 0) does not say anything about loading, only about the number of records present. If you are lucky, at least one record made it to the store, so it is likely the store has done a load.If you are unlucky it has loaded but got zero records back. Your (getCount() > 0) then claims we never did a load. For fun: maybe someone can come up with a while loop where using this construction could cause serious problems.. anyone?? :)

Eugen_
12 Feb 2010, 7:14 AM
<role_list>
<role>
<role_id><![CDATA[1]]></role_id>
<role_title><![CDATA[Administrator]]></role_title>
</role>
<role>
<role_id><![CDATA[2]]></role_id>
<role_title><![CDATA[Doctor]]></role_title>
</role>
<role>
<role_id><![CDATA[3]]></role_id>
<role_title><![CDATA[Patient]]></role_title>
</role>
</role_list>
Let's assume that data required for combobox to create a new user. In that case role list cannot be empty, because each user of system must have some role. We are waiting for 'load' event or store.isLoading() or store.getCount(). There is no difference at all. I talking about that case only.

dlbjr
12 Feb 2010, 7:23 AM
var isLoaded = false;

var ds = new Ext.data.JsonStore({
url: Config.dataURL,
root: 'data',
fields: [
{ name: 'role_id', type: 'string' },
{ name: 'role_desc', type: 'string'}]
});

ds.on('load', function() {
isLoaded = true;
});

ds.load({ params: { did: 3, user: User.Username} })

//Now you can check value of isLoaded any time you want

Mike Robinson
12 Feb 2010, 7:45 AM
At any particular instant, a Store might be busy handling any number of the four "CRUD" request types: Create, Retrieve, Update, Destroy. It might be doling-out those requests one at a time or it might have sent them in large batches. And you really do not want to "tie" your software design to those details, which are by design supposed to be opaque to you.

Events serve two[/i] important purposes in the JavaScript worlds: (1) they are a notification that something of interest to your code has occurred; but also (2) they are an opportunity to execute code at an opportune time. The notification mechanism [U]is "a subroutine call," and that call is made at a point in time when the internal state of the calling object is stable and reliable. There are no messy "mutexes" and "tasks" and such: this is not a UNIX "signal." Although the event occurs asynchronously from the point-of-view of human time, it is synchronous with respect to the internal state of ExtJS which issued the call.

If you are "looking for a flag," there are a couple of subtle risks. One is that you are "busy waiting" on that flag. Another is that you might react to it at the wrong moment. Although JavaScript does not support "co-routines," lesser forms of inconsistency could happen.

Submit your request-object and put whatever additional attributes you need for your housekeeping purposes when the event-handling routine is called. This (JavaScript) strategy is robust and stable: follow it.

realjax
13 Feb 2010, 1:27 AM
There is no difference at all.

Fine, suit yourself.

mankz
13 Feb 2010, 8:31 AM
Check Store for lastOptions, if that exists then your Store has loaded at least once.

TigrouMeow
8 Sep 2011, 5:43 PM
There is no difference at all.

Suit yourself.

Haha! I tend to agree with realjax. It's simply not proper, and, even though it could work for a while (in the logic of the current developer), later on the code could be changed (or the database), and the numbers of results could be 0. A new developer wouldn't know, and would assume that IsLoaded is doing its job, but it actually wouldn't.

A little hack on the store to turn this attribute after a successful loading is a much better way. It's not hard enough to implement to look for another solution.

stevanicus
26 Jul 2012, 6:03 AM
Check Store for lastOptions, if that exists then your Store has loaded at least once.

Thank you, worked like a treat! I'm not sure if this is version specific (4.1.1rc-2) but .on("load") only works when the store is loaded and I had trouble running through the same code twice as the event isnt fired again (obviously) :)

landoni
6 May 2013, 6:35 AM
This also worked for me, thanks. Why is it not in the docs? or am i missing something?

Agustin