From 92ba3dd98a1ff37437f4d628874d71d46f600965 Mon Sep 17 00:00:00 2001 From: bjorn Date: Wed, 29 Jul 2026 16:31:04 +0200 Subject: [PATCH 1/2] docs: make the asynchronous create demo work on right click The demo opened its menu from a `mouseup` handler, which is not the event you get when the user right clicks. Chromium keeps its own context menu open over the page and never delivers the `mouseup`, so only the native menu showed up. Firefox delivers it, but only after the native menu is already on screen. Bind `contextmenu` instead and call `preventDefault()` on it so the browser menu stays away. The items are now handed to a callback from a `setTimeout`, so the demo really is asynchronous rather than building the items inline and merely deferring the open. Because `$.fn.contextMenu()` opens the menu by triggering a `contextmenu` event on the trigger, the handler runs again. A flag on the trigger makes that re-entrant call a no-op, and it doubles as a guard against starting a second request while one is still in flight. The spec file was renamed from `aync-create.js` to `async-create.js` and extended to cover the right click flow: the menu only appears after the delay, the native menu is prevented, the menu also opens from a `contextmenu` event that is never followed by a `mouseup`, the re-entrant event settles instead of recursing, and a second right click still works. --- documentation/demo/async-create.md | 88 ++++++++++++++++++--------- test/specs/async-create.js | 98 ++++++++++++++++++++++++++++++ test/specs/aync-create.js | 11 ---- 3 files changed, 157 insertions(+), 40 deletions(-) create mode 100644 test/specs/async-create.js delete mode 100644 test/specs/aync-create.js diff --git a/documentation/demo/async-create.md b/documentation/demo/async-create.md index 3376fc15..050edde6 100644 --- a/documentation/demo/async-create.md +++ b/documentation/demo/async-create.md @@ -13,50 +13,80 @@ currentMenu: async-create +Sometimes the menu items are not known up front, for example because they have to be +fetched from the server first. This demo registers the menu with `trigger: 'none'` and +opens it by hand once the items have arrived. The `setTimeout` below stands in for that +server round trip, so the menu appears about a second after you right click. + +Two things are worth pointing out: + +* The `contextmenu` event is handled directly on the trigger and `preventDefault()` is + called on it, otherwise the browser shows its own context menu while you are still + waiting for the items. +* `$.fn.contextMenu()` opens the menu by triggering a `contextmenu` event on the element, + which runs the very same handler again. The `asyncMenuBusy` flag below makes that + re-entrant call a no-op, and it doubles as a guard against firing a second request while + one is still in flight. + right click me ## Example code