问题描述
我正在处理的 Node.js 应用程序的客户端代码中遇到问题.
这背后的想法是在套接字接收到事件时立即更新浏览器.
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.
这是客户端的脚本块:
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);
});
当 alert()
放在 jade.render()
之前时,它会发出警报,但如果插入之后,它不会执行(因此,我认为这是 jade.render()
的问题.
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()
).
这是 objTemplate.jade,在第 7 行中引用:
This is objTemplate.jade, referred to in line 7:
p #{object}
// That's it.
这是来自 app.js 的相关片段:
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);
});
更新:
这是 IMO 的 /public/scripts/jadeTemplate.js
链接对于一个问题来说太长了.
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. :)
推荐答案
你的模板不需要属性 object
,它需要 obj
.试试这个:
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/ 显示它有效.如果您将属性改回 object
,您将在控制台中收到一个 javascript 错误.
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.
这篇关于ClientJade - 无法执行 jade.render()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!