ol-contextmenu icon indicating copy to clipboard operation
ol-contextmenu copied to clipboard

Allow on open method to be asynchronous

Open flyfishMT opened this issue 7 years ago • 3 comments

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.

flyfishMT avatar Apr 10 '18 21:04 flyfishMT

Yeah, this is a great improvement. It has to be done. Would you like to provide a PR?

jonataswalker avatar Apr 11 '18 12:04 jonataswalker

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.

flyfishMT avatar Apr 11 '18 16:04 flyfishMT

Yeah, Promises is the way to go.

jonataswalker avatar Apr 12 '18 10:04 jonataswalker