Search code examples
orbeonxforms

Orbeon 2018.1 cannot access ORBEON object from html/head/script


As of Orbeon 2018.1 something happened in how the ORBEON javascript object is loaded. The stuff below used to work on versions 2017.2.2 and 3.9.

Objective: I want to load the TinyMCE widget using the appropriate language. To that end I used to have code like this:

<xh:script type="text/javascript">
    var lang = ORBEON.xforms.Document.getValue('language') ? ORBEON.xforms.Document.getValue('language').split('-')[0] : 'en';
    var TINYMCE_CUSTOM_CONFIG = {
        language:                  lang,
        ...

This html/head code fails in version 2018.1 because the ORBEON object is "undefined".

I tried moving the variable lang to my model like so:

<xf:instance id="language">
    <language/>
</xf:instance>
<xf:action ev:observer="language" ev:event="xxforms-value-changed">
    <xxf:script>
        var lang = ORBEON.xforms.Document.getValue('language') ? ORBEON.xforms.Document.getValue('language').split('-')[0] : 'en';

        TINYMCE_CUSTOM_CONFIG.language = lang;
    </xxf:script>
</xf:action>

But now the code fails because it says "java.lang.IllegalArgumentException: Cannot find control for id language"

What am I missing here?


Solution

  • In general, it is dangerous to assume that everything has been loaded when reaching an inline script. Better defer the JavaScript initialization to a later time.

    By default, Orbeon Forms 2018.1 no longer includes inline scripts and therefore JavaScript initialization is deferred. This was mainly done to help with the CSP header. You can re-enable inline scripts with the property (see doc), but it might still be better to defer your own initialization to a later time.