Allow on open method to be asynchronous
Is there any way for the "on open" handler to happen asynchronously? I am working with a TileWMS source. So, I just have an image representation of the map, and to get feature information I have to query a web service (WMS).
var menu = new ContextMenu({
width: 180,
items: contextMenuitems
});
var ctx_open = function (evt) {
$.ajax({
url: getWMSFeatureInfoUrl,
dataType: 'json',
contentType: "application/json; charset=utf-8"
}).then(function (data, textStatus, jqXHR) {
if (data && data.features && data.features.length > 0) {
// custom context menu when clicking on a feature
} else {
// show standard context menu
}
});
menu.on("open", ctx_open.bind(this));
So the "on open" handler returns before I've returned from the service and the original context menu will momentarily show. Is there a way for the open method to be "awaited" - return a promise or provide a callback?
I am new to both openlayers and the gis backend so I apologize for any inaccuracies.
Yeah, this is a great improvement. It has to be done. Would you like to provide a PR?
I can take a stab at it but might need some guidance. I would think all handlers should work this way? I just looked at it briefly and it looks like they're called with Base.DispatchEvent which is an openlayers method.
Yeah, Promises is the way to go.