Demo: Create Context Menu (asynchronous)
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
contextmenuevent is handled directly on the trigger andpreventDefault()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 acontextmenuevent on the element, which runs the very same handler again. TheasyncMenuBusyflag 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.