Accessing @root from a component within #each



  • I'm having trouble accessing the "real" root object with @root in Handlebars. I'm only getting this when asking for @root. I suspect that this has something to do with my dynamic invocation of my components.

    I have a helper like this:

    function renderLayout(shortid, options) {
      if(!shortid || typeof shortid !== 'string') return null;
      let context = options.hash ? options.hash : {};
    
      return componentById.call({
        ...this,
        ...context
      }, shortid, options)
    }
    

    The only way I can access root from my components is by excplicitly adding root to the call, like this:

    const jsreport = require('jsreport-proxy');
    
    function renderLayout(shortid, options) {
      if(!shortid || typeof shortid !== 'string') return null;
      let context = options.hash ? options.hash : {};
    
      return componentById.call({
        ...this,
        ...context,
        root: jsreport.req.data
      }, shortid, options)
    }
    

    componentByIdlooks like this:

    async function componentById(shortid) {    
        const component = await jsreport.documentStore.collection('components').findOne({
            shortid
        })
    
        if(!component) throw new Error(`Component ${shortid} not found`)
    
        return await jsreport.templatingEngines.evaluate({
          engine: component.engine,
          content: component.content,
          helpers: component.helpers,
          data: this
        }, {
          entity: component,
          entitySet: 'components'
        })
    }
    

    Is this how I have to solve this, or is there a better solution?



  • I solved this by adding this global helper:

    function getRoot() {
      return jsreport.req.data;
    }
    

    Is this an ok solution, or should I do it any other way?


Log in to reply
 

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