I thought this would be very straight forward, but it has turned into an extremely challenging task. I am using the nearme widget to capture features from a polygon layer. The resulting feature attributes can then be used as a filter for an attribute query in a web form. I just need to capture the features, loop through that attributes and return an array of id's, one for each feature. Sounds simple enough, except that it can take up to 8 seconds for the results to populate, depending on the size of the buffer used. I am trying to set up a promise function to wait for the results, and then once captured I can loop through the array and return the ids. I can't seem to get this to work, I just undefined. I think the problem is that it is hard to determine exactly when the results have finished populating. This is what I have tried:
_getResultsArray: function(itemList) {
console.log("item list object ", itemList);
var wmaArray = null;
var def = new Deferred();
if (itemList) {
wmaArray = itemList._searchedFeatures['Wildlife Management Areas'];
console.log("WMAs ", wmaArray);
def.resolve(wmaArray);
}
console.log("wma array ", def);
return def.promise;
},
this._getResultsArray(this._itemListObject).then(lang.hitch(this, function(wmaArray){
console.log("WMAs ", wmaArray);
}));
I have also tried this without getting any result:
this.own(on(this._itemListObject, "load", lang.hitch(this, function () {
var wmaArray = itemList._searchedFeatures['Wildlife Management Areas'];
console.log("WMAs ", wmaArray);
})));
This only way I can get the results is by setting a timeout, but this is not a good solution. Even if I set it for 8 seconds, I can still get be undefined if there are a lot of returns! Additionally, if there are only a couple of results, that can be a big problem as well.
setTimeout(function() {
var wmaArray = itemList._searchedFeatures['Wildlife Management Areas'];
console.log("WMAs ", wmaArray);
}, 8000);
Can anyone see what is wrong with the first solution? Am I going about the the wrong way?
Thanks for any advice.