nowjs的正确使用方法

发布于 2024-11-08 05:08:08 字数 771 浏览 2 评论 0原文

这是我目前使用nowjs的方式,但是这种使用方式是错误的吗?

客户端:

$('#guestbook_form').validate({
    rules: {
        message: {
            required: true, maxlength: 1000
        }
    },
    submitHandler: function(form) {
        now.sendGb($(form).serializeObject());
    }
});

now.pushMessage = function(message) {
    $('#guestbook_records').append(message);
};

服务器:(

everyone.now.sendGb = function(message) {
    client.query(
        'INSERT INTO guestbook SET owner_id = ?, user_id = ?, message = ?',
        [message.id, message.user_id, sanitize(message.message).entityEncode().trim()]
    );
    everyone.now.pushMessage(message.message);
};

当然,此代码还有一些其他问题,但仅作为示例)

我还应该如何使用 nowjs?

感谢您的帮助

This is currently the way i use nowjs, but is this wrong way to use it?

client:

$('#guestbook_form').validate({
    rules: {
        message: {
            required: true, maxlength: 1000
        }
    },
    submitHandler: function(form) {
        now.sendGb($(form).serializeObject());
    }
});

now.pushMessage = function(message) {
    $('#guestbook_records').append(message);
};

server:

everyone.now.sendGb = function(message) {
    client.query(
        'INSERT INTO guestbook SET owner_id = ?, user_id = ?, message = ?',
        [message.id, message.user_id, sanitize(message.message).entityEncode().trim()]
    );
    everyone.now.pushMessage(message.message);
};

(Of course theres a few other problems with this code, but but just as an example)

How else should i use nowjs?

Thanks for helping out

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

淡看悲欢离合 2024-11-15 05:08:08

只是为了一般的稳健性,我建议

everyone.now.sendGb = function(message) {
    client.query(
        'INSERT INTO guestbook SET owner_id = ?, user_id = ?, message = ?',
        [message.id, message.user_id, sanitize(message.message).entityEncode().trim()],
        function(err, info) {
            if (!err) {
                everyone.now.pushMessage(message.message);
            }
        }
    );
};

在将消息发布给每个人之前仔细检查插入是否发生且没有错误。

Just for general robustness I would recommend

everyone.now.sendGb = function(message) {
    client.query(
        'INSERT INTO guestbook SET owner_id = ?, user_id = ?, message = ?',
        [message.id, message.user_id, sanitize(message.message).entityEncode().trim()],
        function(err, info) {
            if (!err) {
                everyone.now.pushMessage(message.message);
            }
        }
    );
};

This double checked that the INSERT happened without an error before posting the message to everyone.

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