georgesamper
georgesamper

Reputation: 5169

Right way to use 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

Upvotes: 2

Views: 932

Answers (1)

Raynos
Raynos

Reputation: 169373

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.

Upvotes: 2

Related Questions