I am trying to pass a variable from the route handler to the javascript file.
express route that fetches home page is:
exports.home = function(req, res){
var _ajaxData = [ ["#collections","collections", "Collections"],
["#candidates","candidates", "Candidates"],
["#entries","entriess", "Entries"],
["#exits","exits", "Exits"]
];
res.render('home/home', { title: 'Welcome to My Web', _ajaxData: _ajaxData});
};
And my jade file looks like the following:
extends ../layout/base
block content
each item in _ajaxData
div(id= item[0]).col-md-6
h3.panel-title= title
.panel-body
| Loading content...
script(src='js/home.js')
And the content is loaded using ajax in home.js that looks like the following:
var ajaxData = JSON.stringify('!{_ajaxData}');
console.log(ajaxData);
// Further processing here
Problem: The console prints:
"!{_ajaxData}"
where I am looking to get the full array passed to the javascript.
Appreciate any insights
$ and $$ are valid variable names in JavaScript, they have no special meaning. Usually they set their value to library instances, in your example if you check the closure call, at the end of the file you'll see that $ is jQuery in this case if it is defined and $$ is cytoscape.
There are two ways to pass variables between web pages. The first method is to use sessionStorage, or localStorage. The second method is to use a query string with the URL.
Jade is a template engine for node. js and the default rendering engine for the Express web framework. It is a new, simplified language that compiles into HTML and is extremely useful for web developers. Jade is designed primarily for server-side templating in node.
Add this to your jade file
script(type='text/javascript')
var local_data =!{JSON.stringify(data)};
script(src='javascripts/myScript.js')
All your data should now be in local_data
for your use in myScript.js
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With