Loading multiple assets from helper function instead of {{asset ..}}



  • I need to load fonts and other styles conditionally depending on some settings in the report data. I could to this with if statements in handlebars, but I'd rather handle the conditions in JavaScript.

    Here is a simplified function which loads two css files. I invoke this by putting {{loadStyle}} in the <Style> tag.
    jsreport is required earlier in the shared helper where this code lives.

    async function loadStyle () {
      const loadAssets = [];
      loadAssets.push(jsreport.assets.read('ddp-loadfont-default.css', 'utf8'))
      loadAssets.push(jsreport.assets.read('ddp-common.css', 'utf8'))
    
      const assets = await Promise.all(loadAssets);
      return assets.join('\n');
    }
    

    This seems to work fine, but I wonder if I also should await jsreport.assets.evaluateShared() as you do in the original code for the asset helper? In that case, where should I call evaluateShared?

    https://github.com/jsreport/jsreport/blob/master/packages/jsreport-assets/static/helpers.js



  • The shared helpers are evaluated just once. It means you can skip it and your current code should be just fine.



  • Thank you for a speedy saturday-answer.
    Have a nice weekend.


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.