玉+ EXPRESS:在内联 JS 代码(客户端)中迭代对象?
我想实现一个基于其API的谷歌地图。我想添加一个基于坐标的路径。因此,我从模型中获取坐标,并希望迭代该对象以用这些点填充地图。在我的 jade 模板中,我包含这样的 api js 代码:
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;')
问题是:jade 呈现此代码片段
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)
];
,因为它在 jade 模板源中... - if 等不会被解析!有什么想法吗?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
整个脚本标签(其下缩进的所有内容)将通过原始数据传递,而无需进一步解析。 Jade 进行 HTML 模板化,而不是 HTML 模板化加上嵌套的 javascript 模板化。要将您的
pins
变量从玉本地模板变量数据传递到脚本源代码,您必须采取其他一些方法,例如使用原始玉来呈现一个只调用您的initialize 的小脚本标记
函数将pins
数据作为文字,或者将您的pins
数据粘贴到 dom 的某个位置,然后从那里读取它。脚本标记下方的这些内容(伪代码,尚未测试)The 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 yourinitialize
function with thepins
data as a literal, or stick yourpins
data into the dom somewhere and then read it from there. Something along these lines below your script tag (pseudocode, haven't tested)我将值作为隐藏输入元素传递,例如:
通过 jQuery 访问:
Joe
I passed the value as a hidden input element, e.g.:
Accessed via jQuery:
Joe
您可以使用这个:
You can use this:
脚本标签是纯文本,你不能轻易地混合 Jade 来生成这个 javascript,这通常是糟糕设计的反映。您可以将客户端需要的内容序列化为 JSON,或者使用 express-expose 来执行此操作
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
client side
我有一个类似的问题,这行代码解决了它:
答案最初来自 这篇文章
I had a similar issue and this line of code solved it:
The answer came originally from this post