Custom Event Dynamic Action based on $(document) jQuery selector

Drumbeg picture Drumbeg · Feb 16, 2015 · Viewed 15.2k times · Source

I've been using dynamic actions based on custom events in one of my Oracle Apex pages. I am binding my event to the document and then using a 'DOM Object' selection type (of document) in order to specify the context for the event.

enter image description here This works in Apex 4.2, however I have just come across this in relation to Apex 5:

https://docs.oracle.com/cd/E59726_01/doc.50/e39143/toc.htm#BAJDAGJG

5.10 Deprecation of Dynamic Actions Based on DOM Objects

Dynamic actions based on DOM Objects have been deprecated. Change your dynamic actions to use a jQuery Selector or JavaScript Expression instead of DOM Object.

My question is, how can I use a jQuery selector in order to detect events bound to the document? When I try using a jQuery selector of document, the dynamic action does not fire. I strongly suspect that this is because APEX wraps the selector in quotes when the dynamic action is parsed, rendering it useless for selectors on the document or window objects.

I am already aware that in the standard jQuery world I would just use $(document).

enter image description here

I already know that I can bind events to different DOM elements. I'm not interested in that. I am interested specifically in binding to document.

Answer

Bacs picture Bacs · Feb 19, 2015

jQuery selectors return element nodes. Your event is bound to the document node, so there's no way to get at it with a jQuery selector. $(document) is not strictly speaking a selector. I believe $(":root").parent() returns the document object but that doesn't help you, since Oracle only lets you use selectors, not methods.

Oracle got back to me earlier with my Apex 5 workspace, so I've been having a play. The solution is in the documentation you quoted. You can't use a jQuery selector in your dynamic action's Selection Type, but you can simply use a Javascript Expression, with the value: document

I tested this by creating a button pointing to the URL:

javascript:apex.event.trigger(document,'testEvent');

I created a dynamic action responding to the Custom Event testEvent, Selection Type Javascript Expression, expression value document. It works fine, and the button now triggers an alert via a custom event handled at the document.