Search code examples
dojohttp-headersdojox.grid

Dojo datagrid jsonrest response headers


I'd like to use custom headers to provide some more information about the response data. Is it possible to get the headers in a response from a dojo datagrid hooked up to a jsonRest object via an object store (dojo 1.7)? I see this is possible when you are making the XHR request, but in this case it is being made by the grid.

The API provides an event for a response error which returns the response object:

  on(this.grid, 'FetchError', function (response, req) {
      var header = response.xhr.getAllResponseHeaders();
  });

using this I am successfully able to access my custom response headers. However, there doesn't appear to be a way to get the response object when the request is successful. I have been using the undocumented private event _onFetchComplete with aspect after, however, this does not allow access to the response object, just the response values

aspect.after(this.grid, '_onFetchComplete', function (response, request) 
{
 ///unable to get headers, response is the returned values
}, true);

Edit: I managed to get something working, but I suspect it is very over engineered and someone with a better understanding could come up with a simpler solution. I ended up adding aspect around to allow me to get hold of the deferred object in the rest store which is returned to the object store. Here I added a new function to the deffered to return the headers. I then hooked in to the onFetch of the object store using dojo hitch (because I needed the results in the current scope). It seems messy to me

aspect.around(restStore, "query", function (original) {
    return function (method, args) {
        var def = original.call(this, method, args);
        def.headers = deferred1.then(function () {
            var hd = def.ioArgs.xhr.getResponseHeader("myHeader");
            return hd;
        });
        return def;
    };
});

aspect.after(objectStore, 'onFetch', lang.hitch(this, function (response) {
    response.headers.then(lang.hitch(this, function (evt) {
        var headerResult = evt;
    }));
}), true);

Is there a better way?


Solution

  • I solved this today after reading this post, thought I'd feed back.

    dojo/store/JsonRest solves it also but my code ended up slightly different.

    var MyStore = declare(JsonRest, {
        query: function () {
            var results = this.inherited(arguments);
            console.log('Results: ', results);
            results.response.then(function (res) {
                var myheader = res.xhr.getResponseHeader('My-Header');
                doSomethingWith(myheader);
            });
            return results;
        }
    });
    

    So you override the normal query() function, let it execute and return its promise, and attach your own listener to its 'response' member resolving, in which you can access the xhr object that has the headers. This ought to let you interpret the JsonRest result while fitting nicely into the chain of the query() all invokers.

    One word of warning, this code is modified for posting here, and actually inherited from another intermediary class that also overrode query(), but the basics here are pretty sound.