How to create common script file in jsreport if same script file is referring in multiple templates?
-
Hi sir,
I have read script extension of jsreport from documentation .
-
template - run for templates where its explicitly referenced
-
global - run for all templates
-
folder - run for all templates in the same folder hierarchy
Also i have seen samples templates in jsreport .In those sample templates only one script is used in each templates and if there is any common function is there in any templates those function will be written in global script(global helper).
But in my case i have one template for invoice report (Template consist of html, css and script file) and functions written inside beforeRender function of script is common for multiple templates .So how to write function inside beforeRender function
in common script file and how to import ?//Suppose i have 3 templates Invoice1 (whose script file is Invoice1.js),Invoice2 (whose script file is Invoice2.js) and Invoice3 (whose script file is Invoice3.js).
//1.Invoice1.js
function beforeRender(req,res){
callFunctionA(req,res)
}function afterRender(req,res){
}
//1.Invoice2.js
function beforeRender(req,res){
callFunctionA(req,res)
}function afterRender(req,res){
}
//1.Invoice3.js
function beforeRender(req,res){
callFunctionA(req,res)
}function afterRender(req,res){
}
//Then create a common script file because those bussiness login which written inside beforeRender function is common for those 3 templates and to avoid redundency as well as if there is any change in our bussiness logic we can only modify this common script file instead of modifying 3 template script file.
function callFunctionA(req,res){
//this function will call so many nested function and global helpers
}Can you please show me one example ?
Also what i understood about Global helper is that it is for writing any common function which will be available for all templates but in my case it is not about a particular single function it will have so many calculations,it will call nested functions,global helpers etc and businnes logic will be different for different templates.Also i have create my common script file as global helper but it will be avaiable for all templates and i don't want that.
can u please suggest me any solution for achieveing this scenario?
-
-
hi @reshmajacob93 yes, the case you are describing is better solved by creating a module and reusing it by import it in each script rather than a global script. you can achieve the creation of module, import and reuse by using asset.
here is a basic example that shows how to share logic. hope it helps you
-
Thank you sir