玉+ EXPRESS:在内联 JS 代码(客户端)中迭代对象?

发布于 2024-11-14 05:45:13 字数 1343 浏览 2 评论 0原文

我想实现一个基于其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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

原来是傀儡 2024-11-21 05:45:13

整个脚本标签(其下缩进的所有内容)将通过原始数据传递,而无需进一步解析。 Jade 进行 HTML 模板化,而不是 HTML 模板化加上嵌套的 javascript 模板化。要将您的 pins 变量从玉本地模板变量数据传递到脚本源代码,您必须采取其他一些方法,例如使用原始玉来呈现一个只调用您的 initialize 的小脚本标记 函数将 pins 数据作为文字,或者将您的 pins 数据粘贴到 dom 的某个位置,然后从那里读取它。脚本标记下方的这些内容(伪代码,尚未测试)

- 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>"

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 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>"
囚你心 2024-11-21 05:45:13

我将值作为隐藏输入元素传递,例如:

    input(type='hidden', id='variableName', value='#{variableName}')

通过 jQuery 访问:

    $("#variableName").val()

Joe

I passed the value as a hidden input element, e.g.:

    input(type='hidden', id='variableName', value='#{variableName}')

Accessed via jQuery:

    $("#variableName").val()

Joe

天赋异禀 2024-11-21 05:45:13

您可以使用这个:

script
  console.log(#{var_name});

You can use this:

script
  console.log(#{var_name});
抚你发端 2024-11-21 05:45:13

脚本标签是纯文本,你不能轻易地混合 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

茶底世界 2024-11-21 05:45:13

服务器端

JSON.stringify(users)

客户端

var users=JSON.parse(("#{users}").replace(/"/g,'"'));

server side

JSON.stringify(users)

client side

var users=JSON.parse(("#{users}").replace(/"/g,'"'));
浅浅淡淡 2024-11-21 05:45:13

我有一个类似的问题,这行代码解决了它:

var myvar  = !{JSON.stringify(mydata)};

答案最初来自 这篇文章

I had a similar issue and this line of code solved it:

var myvar  = !{JSON.stringify(mydata)};

The answer came originally from this post

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文