开始使用now.js框架。使用now.js中的示例代码,我正在尝试实现一个聊天。包括这里的完整性。
<script src="http://localhost:8080/nowjs/now.js" type="text/javascript"></script>
<!-- HTML for Chat Here -->
$(document).ready(function() {
now.receiveMessage = function(name, message) {
$(".chattext").append("<br>" + name + ": " + message);
$(".chattext").attr({ scrollTop: $(".chattext").attr("scrollHeight") });
}
$("#send-button").click(function() {
now.distributeMessage($("#text-input").val());
$("#text-input").val("");
});
now.name = prompt("What's your name?", "")
});我已经让node.js服务器与now.js正常工作。
我试图扩展聊天,以便当用户输入其名称时,会向服务器发送一条消息,指出“现在已加入聊天”。示例代码提示用户输入名称,并将其设置为now对象的名称。
now.name = prompt("What's your name?", "");此时,now对象是可用的。因此,我不只是简单地设置now.name,而是尝试设置now.name并通过调用distributeMessage发送消息(‘John已经加入聊天’)。
var p = prompt("What's your name?", "");
if (!p) {
// errs here
} else {
now.name = p;
now.distributeMessage(now.name + " has joined chat.");
}Chrome和firefox报告了一个错误:
对象#没有“分发消息”方法.
我不明白为什么。可以设置now.name属性。控制台日志显示带有get distributeMessage和设置distributeMessage函数的对象。当我点击“发送按钮”元素时,我可以发送消息。但是,我现在还不能给distributeMessage打电话。
当我尝试进行方法调用时,now对象是否未完全加载?我需要包含某种“现在”的onLoadReady方法吗?在提示符之后,我需要做什么才能触发now.distributeMessage方法?
发布于 2011-12-15 18:12:02
我找到了解决我的问题的方法。我需要用一个
now.ready(function() {
now.distributeMessage(now.name + " has joined chat.");
})现在看来客户端名称空间是可用的,但是现在所有的服务器端调用仍然需要javascript来完成处理。
https://stackoverflow.com/questions/8513430
复制相似问题