shaithana
shaithana

Reputation: 2490

Handlebars and Javascript

It's a good practice to include js file as partials in Handlebars? I mean, I need to use some data from nodejs in my jquery-based javascript, and to do this I load my js as partials in handlebars template and then use handlebars notation directly, this way:

$(document).ready(function() {
    var myVar = {{value_from_db}};
});

I know this it's not the correct way, I hope I've explained what I need.

In my case, I need to use MapBox coordinates inside the js, where the coordinates are get from database, this way:

var map = L.mapbox.map('map', 'mapbox.streets', {
        zoomControl: false,
        attributionControl: false
    }).setView([my_lat, my_lng], 3);

Upvotes: 2

Views: 194

Answers (1)

Atilla Ozgur
Atilla Ozgur

Reputation: 14721

If your value_from_db is unique ok, otherwise "No. Do not do this."

When your js files are static, browser will cache them and do not download them again. But your js files are changing, how will you tell browser your file changed. Implementing this logic will be hard for you to do.

a.js , v1

$(document).ready(function() {
    var myVar = 20;
});

a.js , v2

$(document).ready(function() {
    var myVar = 30;
});

Upvotes: 2

Related Questions