Search code examples
javascriptdojodojox.grid

Why Dojo grid.refresh is making another server call?


We are using dojo JsonRest. Whenever we need to refresh the grid with new data, dojo is firing two server calls.

Below is the code.

var MyJsonRest = declare(JsonRest, {
   get: function(id, options) {
       return this.inherited(
           arguments, 
           [id, lang.mixin(this.headers, options)]);
   }
});

myDataStore = MyJsonRest({
    target: someurl,
   headers: { 
      'moduleUName': somemodulename
   },
   idAttribute: "id",
       query: function(query, options) {
          // some other code                    
      }                   

});

myDataStore.get("", { paramName: paramValue }).then(function(result) { // this fires a request to server
  gridObj.refresh(); // this fires same request 2nd time to the server
  // if the gridObj.refresh() is commented out, then the grid does not displayes the new data.
});

Solution

  • In order for the Grid to detect changes in the store without triggering the refresh, you should wrap your JsonRest store with the dojo.store.Observable. Any updates to the store will then be auto updated in the grid.

    myCacheDataStore = new Cache(myDataStore, new Memory({});
    myObservableDataStore = new Observable(myCacheDataStore)    
    

    You should be able to pass myObservableDataStore in place of myDataStore.

    To update or refresh the grid you can change the target url or update the headers in myDataStore directly and call grid.refresh. I have not tested with the updating of headers, but my example changes the target url from

    /rest/users/ 
    

    to

    /rest/users/?username=jdoe 
    

    and the grid updates fine.

    myDataStore.target = '/rest/users/?username=jdoe'
    

    or maybe

    myDataStore.headers[paramName] = paramValue
    

    since your MyJsonRest store mixin the options provided into "get" into the headers as well.