global javascript file and handlebar helpers



  • I saw this and was excited for the fix. However, it appears I'm already at 7.1. Any other thoughts?



  • Are you sure? Can you check you have there this change?



  • If yes, please email me or @bjrmatos the export zip if it is possible.



  • Okay, I was going merely by the .1.0 versions I saw and I had the latest one. I updated but now I get a different error.

    I can get my report header to work but the main report won't work now and it used to run on its own.

    Error occured - Error during rendering report: Unexpected token } in JSON at position 2144
    Stak - SyntaxError: Unexpected token } in JSON at position 2144
    at JSON.parse (<anonymous>)
    at C:\dev\src\apps\JsReports\node_modules\jsreport-resources\lib\resources.js:37:21
    at Array.forEach (<anonymous>)
    at C:\dev\src\apps\JsReports\node_modules\jsreport-resources\lib\resources.js:36:15
    at tryCatcher (C:\dev\src\apps\JsReports\node_modules\bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (C:\dev\src\apps\JsReports\node_modules\bluebird\js\release\promise.js:512:31)
    at Promise._settlePromise (C:\dev\src\apps\JsReports\node_modules\bluebird\js\release\promise.js:569:18)
    at Promise._settlePromise0 (C:\dev\src\apps\JsReports\node_modules\bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (C:\dev\src\apps\JsReports\node_modules\bluebird\js\release\promise.js:693:18)
    at Promise._fulfill (C:\dev\src\apps\JsReports\node_modules\bluebird\js\release\promise.js:638:18)
    at PromiseArray._resolve (C:\dev\src\apps\JsReports\node_modules\bluebird\js\release\promise_array.js:126:19)
    at PromiseArray._promiseFulfilled (C:\dev\src\apps\JsReports\node_modules\bluebird\js\release\promise_array.js:144:14)
    at Promise._settlePromise (C:\dev\src\apps\JsReports\node_modules\bluebird\js\release\promise.js:574:26)
    at Promise._settlePromise0 (C:\dev\src\apps\JsReports\node_modules\bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (C:\dev\src\apps\JsReports\node_modules\bluebird\js\release\promise.js:693:18)
    at Async._drainQueue (C:\dev\src\apps\JsReports\node_modules\bluebird\js\release\async.js:133:16)
    at Async._drainQueues (C:\dev\src\apps\JsReports\node_modules\bluebird\js\release\async.js:143:10)
    at Immediate.Async.drainQueues (C:\dev\src\apps\JsReports\node_modules\bluebird\js\release\async.js:17:14)
    at runCallback (timers.js:789:20)
    at tryOnImmediate (timers.js:751:5)
    at processImmediate [as _immediateCallback] (timers.js:722:5)



  • It seems you have resources associated with the template, but one of the resources include invalid json.



  • yep. I r dummy. I will fix and let you know. Sorry about that. Thanks for all your help!



  • Okay, yes to the dumb resource issue. Also, the new version of js-assets fixed the global.js issue I was having.

    Again, my thanks for all your help!

    edg



  • Rather than make a new topic, again, I will ask a semi-related question here.

    Is there a way to access the localizedResource in the global javascript file? Or do I have to do some init() type initialization? My intent is to make localization easier in the scripts for others who read it. This would allow me to parse the localization file out to the parts we create with better names.

    I guess that brings up, I know there is beforeRender, is there an initialize event to do this?

    Thanks!

    edg



  • What you you mean with "global javascript file"? I'm not sure I get your intentions.



  • I have a js file marked as "shared helpers attached to each template." That allows me to use any functions I create across all the templates.

    Is there an onInit() or similar call I can do so I can pass my localizations ($localizedResource) and have them available in some global variable I set? This is going to allow me to create functions with parameters like getDataColumnName(database, column, value) instead of needing to know the localizedResource file?

    I do read the examples and items on the learn page but it's not clicking with me how I could do this or if it's even possible. I wish I could understand why!

    Thanks!

    edg



  • This is a bit hack. But does it help to reach your goal?
    https://playground.jsreport.net/studio/workspace/VkLWfMyMb/120

    Basically the shared helpers doesn't have an event like onInit. It is just evaluated before templating engine runs. You can use any global code to do the init like recalculating the inputs.



  • Thanks! I think the "this.m.data.$localizedResource" was the main thing I was wanting.

    I appreciate it!

    edg



  • Sigh. More questions. Do you have underscore or lodash in jsReports? How do I use them in a javascript file? This is where WebStorm isn't helping because when I typed _.find(. . . ) it helped me complete that but then jsReports itself says that it doesn't have _ defined. Is it just a matter of putting any extras like that in a <script> area?

    Thanks!

    edg


  • administrators

    you are trying to use lodash/underscore in your shared helper/or any other helper, right?

    in order to user lodash/underscore first you need to allow them in jsreport, to do that you need to set allowedModules config.
    to allow modules for helpers the configuration is tasks: { allowedModules: [value here] }, for jsreport scripts the configuration is scripts: { allowedModules: [value here] }. you can use allowedModules: '*' as a value if you just want to allow everything or if you want to be specific allowedModules: ['lodash'], just be sure to have installed the module that you want to use first (for example doing npm install lodash or for other external dependencies in your project).

    then just require the module in your helper (just like you have done require('handlebars') in the past`.

    // helper
    var _ = require('lodash')
    
    function hasId(data) {
      return _.has(data, ['id']) 
    }
    


  • Or update node version and use native ES8 function instead :)



  • lol - I'm on 8.9.4. Does that have them? If so, how do I get to them in jsReports?

    Again, thanks for your patience with me in all of this!


  • administrators

    lol - I'm on 8.9.4. Does that have them? If so, how do I get to them in jsReports?

    Jan was just recommending that if you have modern node.js you should try to not use lodash/underscore and just use native modern javascript functions like (.find, .includes, keys), but if you really have the need for lodash/underscore (maybe you are going to use other kind of utilities in these libraries) then you should follow the step above that i gave.



  • I installed jquery and put in var $ = require('jquery') and that seems to be fine and recognized.

    Then, in a function, I have $("#testing").innerHTML += "<div>test</div>" but get an error. What did I do wrong?

    Error occured - Error during rendering report: jQuery requires a window with a document
    Stak - C:\dev\src\apps\JsReports\node_modules\jquery\dist\jquery.js:31
    throw new Error( "jQuery requires a window with a document" );
    ^

    Error: jQuery requires a window with a document
    at module.exports (C:\dev\src\apps\JsReports\node_modules\jquery\dist\jquery.js:31:12)
    at Object.stuffItIntoThere (evalmachine.<anonymous>:102:5)
    at eval (eval at createFunctionContext (C:\dev\src\apps\JsReports\node_modules\jsreport-handlebars\node_modules\handlebars\dist\cjs\handlebars\compiler\javascript-compiler.js:254:23), <anonymous>:6:217)
    at prog (C:\dev\src\apps\JsReports\node_modules\jsreport-handlebars\node_modules\handlebars\dist\cjs\handlebars\runtime.js:219:12)
    at execIteration (C:\dev\src\apps\JsReports\node_modules\jsreport-handlebars\node_modules\handlebars\dist\cjs\handlebars\helpers\each.js:51:19)
    at Object.<anonymous> (C:\dev\src\apps\JsReports\node_modules\jsreport-handlebars\node_modules\handlebars\dist\cjs\handlebars\helpers\each.js:61:13)
    at Object.eval [as main] (eval at createFunctionContext (C:\dev\src\apps\JsReports\node_modules\jsreport-handlebars\node_modules\handlebars\dist\cjs\handlebars\compiler\javascript-compiler.js:254:23), <anonymous>:62:31)
    at main (C:\dev\src\apps\JsReports\node_modules\jsreport-handlebars\node_modules\handlebars\dist\cjs\handlebars\runtime.js:173:32)
    at ret (C:\dev\src\apps\JsReports\node_modules\jsreport-handlebars\node_modules\handlebars\dist\cjs\handlebars\runtime.js:176:12)
    at ret (C:\dev\src\apps\JsReports\node_modules\jsreport-handlebars\node_modules\handlebars\dist\cjs\handlebars\compiler\compiler.js:525:21)

    Thanks!

    edg


  • administrators

    you can't use jQuery in a helper file, because the helper runs server side, in which a window, document or the DOM does not exists, that is the reason of your error.

    if you want to use jQuery in the context of the web page that your template outputs, then you must include jquery as an asset inside a script tag, you can find an example of that here



  • Okay, thanks!


Log in to reply
 

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