I have a query (see this jsbin example) to find the percentile rank of a fields value. So it finds all records (N) and then how many records are less than itself (k). Percentile = k/N as a rounded percent.
The math is working correctly, but I would like to use the whole thing a function to which I could pass arguments for field name and field value when you click on features.
My problem is this: I have the listeners for selecting features outside of the AMD module where I have the query. So if the listeners call this percentile function, it is undefined. However, if I move this function outside the AMD module, then query and QueryTask are undefined, because I set those up in the module.
I'm new to Javascript modules, so I'm hoping to find out if there is a simple way I could avoid this problem. I've tried moving the listeners inside the AMD module, but that makes other functions undefined.
I may not be using the word module correctly here. I think what I'm experiencing is a scope problem. I have declared this function in one scope and I'm trying to access it in another. Is that the right way to think about it?
Here is the complete code:
function pctile(pctileField,pctileValue) {
queryTaskN = new QueryTask("http://gis.datadrivendetroit.org/arcgis/rest/services/DetroitNeighborhoodMetrics/FeatureServer/0");
queryN = new Query();
queryN.where = '1=1';
queryTaskN.executeForCount(queryN, storeResultsN);
function storeResultsN(results) {
N = results;
console.log("N:",N);
};
queryTaskN.on("execute-for-count-complete", function() {
queryTaskK = new QueryTask("http://gis.datadrivendetroit.org/arcgis/rest/services/DetroitNeighborhoodMetrics/FeatureServer/0");
queryK = new Query();
queryK.where = '"'+pctileField+'" > ' + pctileValue;
queryTaskK.executeForCount(queryK, storeResultsK);
function storeResultsK(results) {
k = results;
console.log("k:",k);
p = k / N;
console.log(p);
P = p*100;
};
});
};