Login Register

How can I pass an already loaded store to dataGrid without reloading store?

Hi,

I am using a dataGrid with a JsonRestStore, and the grid Layout is created dynamically based on the data from the store as the number of fields may vary.
In order to do this, I do a store.fetchItemByIdentity( etc etc) and pass the fetched Item to my createDynamicLayout(item) function, which analyses the item to determine how many fields it has.
Then I execute the dataGrid definition with the dynamically created Layout. So far so good.

However, the store is loaded twice. Once when I first fetch an item to analyse the data and once upon declaration of the grid.
This is unfortunate, since the store is quite heavy and takes a long time to load.

Is there a way to pass the already loaded store to the grid? I have tried grid.setStore(store), but the grid is not starting up that way.

Here's the code:

// ...

var resourceStore = new dojox.data.JsonRestStore({target:"/resource", schema: resourceSchema, service: resourceService});

dojo.addOnLoad(function(){
  resourceStore.fetchItemByIdentity({ identity: 'whatever', onItem: function (item) {startResourceGrid(item);}});
  // interestingly, this does not return only the one item with the specified identity, but all items in the store.
  // I don't understand why ...

  function startResourceGrid(item) {       
    var resourceLayout = createResourceLayout (item);
       
                        resourceGrid = new dojox.grid.DataGrid({
                                                query: { id: '*' },
// works but loads twice if this is uncommented      store: resourceStore,
                                                structure: resourceLayout,
                                                clientSort: true,
                                                autoHeight: false,
                                                columnReordering: true,
                                                headerMenu: null
                                                },
                                                'resourceGridNode');

                        resourceGrid.setStore(resourceStore); // store loads only once, but grid shows nothing
                        resourceGrid.startup();

}); //eof addOnLoad

How can I pass an already loaded store to dataGrid without reloa

Have you considered loading the grid but not rendering it then doing a query on the store and passing that to the dynamic generator?
Then you would use resourceGrid.setStructure(resourceLayout); the grid would then be rendered after that.

This is untested code for thought:

Create store

var resourceStore = new dojox.data.JsonRestStore({target:"/resource", schema: resourceSchema, service: resourceService});

create grid

resourceGrid = new dojox.grid.DataGrid({
                                                query: { id: '*' },
                                                store: resourceStore,
                                                structure: resourceLayoutDUMMY,  //where resourceLayoutDUMMY is a minimalistic dummy store
                                                clientSort: true,
                                                autoHeight: false,
                                                columnReordering: true,
                                                headerMenu: null
                                                },
                                                'resourceGridNode');

get your "whatever" from store

resourceStore.fetchItemByIdentity({ identity: 'whatever', onItem: function (item) {
resourceLayout = createResourceLayout (item);
});

then update the grid

resourceGrid.setStructure(resourceLayout);

then start the grid up

resourceGrid.startup();

I know this doesn't answer your question exactly but if you are willing to look at other ways it may work.

*edit*
The Grid may need to be resourceGrid.startup(); before you can query the store so it is not loaded twice but you can load it empty or not make it visible or something.

good idea but made it worse

Hi bbarrin,

I liked your idea but now it's worse. The store url is called three times.
1. when I call resourceGrid.startup() with a minimal structure declared.
2. when I do a resourceStore.fetchItemByIdentity({ identity: 'whatever' etc etc)
3. when I do resourceGrid.setStructure(resourceLayout);

It is working though, even if the fetchItemByIdentity still returns the whole store and not the one item defined by identity.
I tried getting my 'whatever' through resourceGrid._by_idty but it doesn't seem to be loaded yet, right after resourceGrid.startup()

hm. any more ideas?

many thanks!
philou

solved: caching query results

Hi bb,

I figured it out going back to my original setup:

Getting an item from a local (i.e. already loaded) JsonRestStore via store.fetchItemByIdentity() always makes a call to the store service url. Apparently this is expected (desired?) behaviour.

There is however the option to let the store cache the query, thereby preventing it from reloading the store many times over, if we don't expect the data provided by the server to have changed (e.g. during the execution if the same script).

So here is how it is set up:

dojo.require('dojox.data.ClientFilter'); // This must be included before ! the JsonRestStore
dojo.require('dojox.data.JsonRestStore');

var resourceStore = new dojox.data.JsonRestStore({target:"/resource", schema: resourceSchema, service: resourceService, , cacheByDefault: true});
//  cacheByDefault is the magic word.

dojo.addOnLoad(function(){
  resourceStore.fetch({onComplete: function (items) {startResourceGrid(items);}});
 // Must be 'fetch" since the store will call 'fetch'. Apparently 'fetchItemByidentity' and 'fetch' don't use same cache.

  function startResourceGrid(items) {       
    var resourceLayout = createResourceLayout (items);
       
                        resourceGrid = new dojox.grid.DataGrid({
                                                query: { id: '*' },
                                                store: resourceStore,
                                                structure: resourceLayout,
                                                clientSort: true,
                                                autoHeight: false,
                                                columnReordering: true,
                                                headerMenu: null
                                                },
                                                'resourceGridNode');
                        resourceGrid.startup();

}); //eof addOnLoad

More info on caching in the docs at dojocampu.org

I have to say however that I had some trouble turning the caching off, in case I do want the store to reload its data.