node.js - How to correctly use JavaScript with Jade working with parameters passed in on render -
so question correct place link external javascript files in child jade template, , passing data (if possible?) these javascript files.
so using express, , rendering jade files passed in data so:
router.get('/', function(req, res, next) { tflsentimentanalysis.fetchtflsentiments(credentials, function(results){ res.render('twitterinsights', results); }); });
the data accessible normal way in twitterinsights.jade using
#{results.attributename}
i have simple jade file, extends jade template, so:
extend layout block content h2 title of page p page contents go here
and need include 2 javascript files. firstly d3.js library in bower_components , secondly own javascript file in public/javascripts.
script(src="/bower_components/d3/d3.min.js") script(src="/public/javascripts/twittervisualisation.js")
where best position in jade template link javascript files?
my javascript file (twittervisualisation.js) requires data passed jade on render.
how best way pass data javascript file?
i have been searching answer while, , surprised it's not common question. in advance.
one possibility:
extend layout block content script. var results = !{json.stringify(results)}; h2 title of page p page contents go here script(src="/bower_components/d3/d3.min.js") script(src="/public/javascripts/twittervisualisation.js")
basically, results
jade variable rendered json string , placed in results
client-side-js variable can use other script(s). don't prefer doing myself (unless have to) because creates global variable (results
).
it depends on internals of twittervisualisation.js
if there's perhaps better way. instance, if file has function call, rewrite this:
extend layout block content script(src="/bower_components/d3/d3.min.js") script(src="/public/javascripts/twittervisualisation.js") script. twittervisualisationfunction( !{json.stringify(results)} ); h2 title of page p page contents go here
the order in render results , load other js files depends on method suits rest of code best.
Comments
Post a Comment