tonytomov/jqGrid — Issue #260: Loading indicator not shown when using function datatype
Author: dlopuch
Created: 2012-02-10 19:43:59+00:00
State: open

When you load your jqgrid with a function (custom function datatype option), there is no loading indicator.

Problem is in the populate() function inside grid.base.js, line 1729 in master branch:
if ($.isFunction(ts.p.datatype)) { ts.p.datatype.call(ts,ts.p.postData,"load_"+ts.p.id); return;}

All of the other datatypes call the beginReq() and endReq() functions to show and hide the loading indicator. Not the function datatype, though. Since these functions are defined inside the jqgrid closure, there's no external way to access them in my custom function.

The trick here is since your custom function is expected to call one of the addXxxData when the data is ready (potentially asynchronously if you're using custom ajax or service calls in your custom function), you can't simply wrap your custom function datatype call with beginReq() / endReq() -- the beginReq() would work fine, but the endReq() may trigger prematurely if your custom function makes an asynchronous call. I recommend just passing endReq as a callback to your custom datatype call, ie something like:
if ($.isFunction(ts.p.datatype)) {
beginReq();
ts.p.datatype.call(ts,ts.p.postData,"load_"+ts.p.id, endReq);
return;}

Then on the documentation page: http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data
specify that the signature of your custom function datatype should be something like:
myCustomDatatype(postData, something, requestDoneCallback)

Questions / Points to consider:
- Are there any scoping concerns with passing a reference to endReq()? I don't think so...?
- It seems that the custom datatype function call is already passing what looks like the ID of the loading indicator element, presumably so you could manually show and hide it? This is a poor decision because beginReq() and endReq() do more than just show/hide the indicator -- they also set state variables.
- Perhaps to make jqgrid functionally backwards-compatible you should just add explicit references to beginReq() and endReq() and leave it up to the custom function to both show and unshow the loading indicator explicitly. This requires more documentation, though, and not very user friendly.