admin管理员组文章数量:1180406
i want to implement a google map based on its api. i want to add a path based on coordinates to it. therefore i get my coordinates from my model and want to iterate over the object to fille the map with this points. in my jade template i include the api js code like this:
script(type='text/javascript')
function initialize() {
var myLatLng = new google.maps.LatLng(0, -180);
var myOptions = {
zoom: 3,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.TERRAIN
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var flightPlanCoordinates = [
- if (typeof(pins) != null)
- var.pins.forEach(function(pin) {
new google.maps.LatLng(pin.latitude, pin.longitude),
- })
new google.maps.LatLng(0,0)
];
var flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
strokeColor: "#FF0000",
strokeOpacity: 1.0,
strokeWeight: 2
});
flightPath.setMap(map);
}
div#map_canvas(style='height: 500px; background-color: #990000;')
the problem is: jade renders this snippet
var flightPlanCoordinates = [
- if (typeof(pins) != null)
- var.pins.forEach(function(pin) {
new google.maps.LatLng(pin.latitude, pin.longitude),
- })
new google.maps.LatLng(0,0)
];
as it is in the jade template source... the - if etc. doesn't get parsed! any ideas?
thanks!
i want to implement a google map based on its api. i want to add a path based on coordinates to it. therefore i get my coordinates from my model and want to iterate over the object to fille the map with this points. in my jade template i include the api js code like this:
script(type='text/javascript')
function initialize() {
var myLatLng = new google.maps.LatLng(0, -180);
var myOptions = {
zoom: 3,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.TERRAIN
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var flightPlanCoordinates = [
- if (typeof(pins) != null)
- var.pins.forEach(function(pin) {
new google.maps.LatLng(pin.latitude, pin.longitude),
- })
new google.maps.LatLng(0,0)
];
var flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
strokeColor: "#FF0000",
strokeOpacity: 1.0,
strokeWeight: 2
});
flightPath.setMap(map);
}
div#map_canvas(style='height: 500px; background-color: #990000;')
the problem is: jade renders this snippet
var flightPlanCoordinates = [
- if (typeof(pins) != null)
- var.pins.forEach(function(pin) {
new google.maps.LatLng(pin.latitude, pin.longitude),
- })
new google.maps.LatLng(0,0)
];
as it is in the jade template source... the - if etc. doesn't get parsed! any ideas?
thanks!
Share Improve this question asked Jun 7, 2011 at 19:52 trnctrnc 21.6k21 gold badges62 silver badges98 bronze badges6 Answers
Reset to default 16The entire script tag (everything indented under it) is going to be passed through raw without further parsing. Jade does HTML templating not HTML templating plus nested javascript templating. To pass your pins
variable from jade local template variable data to script source code, you'll have to do some other approach like using raw jade to render a tiny script tag that just calls your initialize
function with the pins
data as a literal, or stick your pins
data into the dom somewhere and then read it from there. Something along these lines below your script tag (pseudocode, haven't tested)
- if (typeof(pins) != null)
!= "<script type='text/javascript'>"
!= "var pins = [];"
- forEach pin in pins
!= "pins.push(new Pin(" + pin.latitude + ", " + pin.longitude + "));"
!= "initialize(pins);"
!= "</script>"
I passed the value as a hidden input element, e.g.:
input(type='hidden', id='variableName', value='#{variableName}')
Accessed via jQuery:
$("#variableName").val()
Joe
You can use this:
script
console.log(#{var_name});
the script tags are purely text, you can't easily mix the Jade to generate this javascript, it's usually a reflection of poor design. You can just serialize things as JSON that you need on the client or use express-expose to do this
server side
JSON.stringify(users)
client side
var users=JSON.parse(("#{users}").replace(/"/g,'"'));
I had a similar issue and this line of code solved it:
var myvar = !{JSON.stringify(mydata)};
The answer came originally from this post
本文标签: javascriptJADEEXPRESS Iterating over object in inline JS code (clientside)Stack Overflow
版权声明:本文标题:javascript - JADE + EXPRESS: Iterating over object in inline JS code (client-side)? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738157284a2066427.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论