Access other scripts
The docs say you can have global scripts, but how do you access functions defined in those scripts? I tried to add them to the req object in the global script beforeRender, but that didn't stick. Any help is appreciated.
do you mean like defining a helper function in the global script and access it from your template? if so, here is an example -> https://playground.jsreport.net/studio/workspace/SkQYIQhae/9
note that you can achieve the same using assets extension, defining functions in an asset file, and just importing the asset in your template's helpers, here is another example using asset -> https://playground.jsreport.net/studio/workspace/BJP7eMYTe/10
No neither of those are what I am taking about. I have a server-side js library with a bunch of util methods I'd like to call from other scripts. These are not used in the templates themselves, but to generate data for the template.
i'm afraid that there is no way to assign functions in a script and be able to call the function in another script, jsreport for security reasons by default execute scripts in another process, functions can't be passed across these processes, however to do what you want you have some options:
- store your functions in an asset file and just import the asset in every script where you need your functions, example: https://playground.jsreport.net/studio/workspace/BJP7eMYTe/22
- save your functions in a file, and just require that file in your scripts.
var functions = require('./path/to/functions.js')