Search code examples
javascriptjsondojodojo.gridxjsonreststore

Grid with custom JsonRest not loading data


I have grid(gridx/Grid) connected with my custom store which extends dojo/store/JsonRest.

My server call gets success and store.data will loaded properly.

I have my custom query() method in my store(extends JsonRest). Instead of GET request I use POST request in query() method.

query: function(){
//....custom config.......
 var xhrArgs = {
              url: this.url,
              postData: postData,
              handleAs: "json",
              headers: {'Accept':'application/json','Content-Type':'application/json'},
              load: function(data)
              {
            // ....data customization....
                store.data = customizedData;

              },
              error: function(error)
              {
            console.log(error);
              }
        }
var results =  dojo.xhrPost(xhrArgs);

results.then(function(response){

// ....data customization....
    store.data = customizedData;
return customizedData;
});
return QueryResults(results);

}

I have returned dojo/store/util/QueryResults in query() method and my custom function (in then method).All are working fine. Only thing is grid not populated.


Solution

  • Yes, Found it!!!

    Deffered is the culprit!!

    We should return customized data to the QueryResults not xhr object.

    So my query method should be like this

    query: function(){
        //....custom config.......
         var xhrArgs = {
              url: this.url,
              postData: postData,
              handleAs: "json",
              headers: {'Accept':'application/json','Content-Type':'application/json'},
              load: function(data)
              {
            // ....data customization....
                store.data = customizedData;
    
              },
              error: function(error)
              {
            console.log(error);
              }
                }
        var results =  dojo.xhrPost(xhrArgs);
    
    
    //********************************** THIS IS RIGHT WAY ***************
        var resolvedObject = results.then(function(response){
    
        // ....data customization....
            store.data = customizedData;
        return customizedData;
        });
        return QueryResults(resolvedObject);
    }
    

    When we pass resolved object to QueryResults it appends all IterativeMethods like forEach with resolvedObject and returned that object to grid.

    Then only grid iterate the result object.Actually grid does

    store.query().forEach(##ADD ITEMS TO GRID##))