ClientJade - Trouble executing jade.render()

浪尽此生 提交于 2019-12-08 07:02:36

问题


I've run into a problem in the clientside code for a Node.js app I'm working on.
The idea behind this is to update the browser immediately as the socket receives an event.

This is the script block on the client side:

script(src='/scripts/jadeTemplate.js')
script(src='/socket.io/socket.io.js')
script(type='text/javascript')
  var socket = io.connect();
  socket.on('obj', function(obj) {
    var newsItem = document.createElement("item");
    jade.render(newsItem, 'objTemplate', { object: obj });
    $('#newsfeed').prepend(newsItem);
    alert(obj);
  });

When the alert() is placed before the jade.render(), it alerts, but if inserted after, it doesn't execute (hence, I think it's a problem with the jade.render()).

This is objTemplate.jade, referred to in line 7:

p #{object}
// That's it.

And this is a relevant snippet from the app.js:

var server = dgram.createSocket('udp4');
server.bind(41234);
server.on('message', function(buf, rinfo) {
    isOnline = true;
    var message = buf.toString();
    io.sockets.emit('obj', message);
});

UPDATE:

Here's a link to /public/scripts/jadeTemplate.js, which IMO is too long of a snippet for a question.

If I need to provide any more snippets or files let me know. :)


回答1:


Your template doesn't want an attribute object, it wants obj. Try this:

socket.on('obj', function(obj) {
    var newsItem = document.createElement("item");
    jade.render(newsItem, 'objTemplate', { obj: obj }); // changed object to obj
    $('#newsfeed').prepend(newsItem);
    alert(obj);
});

http://jsfiddle.net/trevordixon/VeYBY/ shows it working. If you change the attribute back to object, you'll get a javascript error in the console.



来源:https://stackoverflow.com/questions/15320306/clientjade-trouble-executing-jade-render

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!