Run AMD script after built-in scripts

Post here questions and problems related to editing and publishing DITA content.
rjcbop
Posts: 56
Joined: Wed Aug 08, 2018 10:23 pm

Run AMD script after built-in scripts

Post by rjcbop »

We have some javascript code in an AMD module that needs to run after the built-in oXygen scripts. Specifically, we need some code to run after the expand.js script is loaded so that all expandable elements have been initialized to their default state. Is there some way to do this? It appears as if this would be possible if we could use the 'defer' attribute on the <script> tag for our script, instead of the default 'async' value but there doesn't seem to be a way to configure that. Thanks for any help you can provide.
alin
Site Admin
Posts: 268
Joined: Thu Dec 24, 2009 11:21 am

Re: Run AMD script after built-in scripts

Post by alin »

Hello,

For the WebHelp Responsive output, please have a look at this topic: https://www.oxygenxml.com/doc/versions/ ... s-amd.html

Regards,
Alin
Alin Balasa
Software Developer
<oXygen/> XML Editor
http://www.oxygenxml.com
rjcbop
Posts: 56
Joined: Wed Aug 08, 2018 10:23 pm

Re: Run AMD script after built-in scripts

Post by rjcbop »

Thanks Alin. We've already successfully used the publishing template approach to inject our custom javascript and it works great. We're having a timing issue with our code versus code that's in the built-in javascript files from oXygen, specifically expand.js. I didn't see anything in that topic about timing issues unless I missed something.
alin
Site Admin
Posts: 268
Joined: Thu Dec 24, 2009 11:21 am

Re: Run AMD script after built-in scripts

Post by alin »

Hello,

You could try to list the "expand.js" as a dependency in your module definition. For example:

Code: Select all

define(["expand"], function () {
    console.log("template module loaded");
});
Here is my console output (I have added some log in the expand.js module):
Image

According to the requirejs documentation here (https://requirejs.org/docs/api.html#defdep):
If the module has dependencies, the first argument should be an array of dependency names, and the second argument should be a definition function. The function will be called to define the module once all dependencies have loaded.
Here is another approach you could use:

Code: Select all

define(["require"], function() {
    
    console.log("template module loaded");
    
    require(['expand'], function() {
        console.log("expand dependent function called");
    });
});
Image


Regards,
Alin
Alin Balasa
Software Developer
<oXygen/> XML Editor
http://www.oxygenxml.com
rjcbop
Posts: 56
Joined: Wed Aug 08, 2018 10:23 pm

Re: Run AMD script after built-in scripts

Post by rjcbop »

Thanks Alin. I'd actually tried the dependency module route but then you end up with two loaded versions of the built-in expand script, which causes problems. I managed to work around the issue by adding code to my script that does what I need from the expand script. Thanks again for your suggestions!
rjcbop
Posts: 56
Joined: Wed Aug 08, 2018 10:23 pm

Re: Run AMD script after built-in scripts

Post by rjcbop »

Correction regarding my earlier statement about multiple instances of the script. I mistakenly thought that I needed to specify the relative path to the expand script in the define statement, and should have just specified "expand" as in your example. When specify a relative path, you get a second instance of the script and when you just specify "expand" you only get a single instance which is what is needed. Many thanks again for the help!
Post Reply