简体   繁体   English

逆向工程一些Javascript聊天室代码

[英]Reverse engineering some Javascript Chatroom code

I am trying to understand the code some guy wrote for a chatroom. 我正在尝试理解某人为聊天室编写的代码。 There is one line that I just can't seem to wrap my head around. 我似乎无法绕过一条线。 What is this line doing: 这条线在做什么:

params['id'] = r.insertID;

The line of code is at the end of the following code snippet: 该行代码在以下代码段的末尾:

    $('#submitForm').submit(function(){

        var text = $('#chatText').val();

        if(text.length == 0){
            return false;
        }

        if(working) return false;
        working = true;

        // Assigning a temporary ID to the chat:
        var tempID = 't'+Math.round(Math.random()*1000000),
            params = {
                id          : tempID,
                author      : chat.data.name,
                gravatar    : chat.data.gravatar,
                text        : text.replace(/</g,'&lt;').replace(/>/g,'&gt;')
            };

        // Using our addChatLine method to add the chat
        // to the screen immediately, without waiting for
        // the AJAX request to complete:

        chat.addChatLine($.extend({},params));

        // Using our tzPOST wrapper method to send the chat
        // via a POST AJAX request:

        $.tzPOST('submitChat',$(this).serialize(),function(r){
            working = false;

            $('#chatText').val('');
            $('div.chat-'+tempID).remove();//this is removing the temporary line :)

            params['id'] = r.insertID;
            chat.addChatLine($.extend({},params));
        });

        return false;
    });

MY CURRENT UNDERSTANDING: 我目前的理解:

The previous code is run after the html page is loaded. 上一个代码是在html页面加载后运行的。 The code schedules an event handler for when a user submits a forum with id submitForm . 该代码为用户提交ID为submitForm的论坛时安排了一个事件处理程序。 So far so good. 到现在为止还挺好。 From there, a few verifications are done, and eventually the code gets to actually calling the function that will output the new line of text inside the chatroom chat.addChatLine($.extend({},params)); 从那里开始,进行了一些验证,最终代码开始真正调用该函数,该函数将在聊天室chat.addChatLine($.extend({},params));输出新行文本chat.addChatLine($.extend({},params)); .

The params['id'] value would later be used in the function chat.addChatLine to identify each <div>....</div> that is being placed into the chat. params['id']值稍后将在chat.addChatLine函数中使用,以标识放置在聊天中的每个<div>....</div>

QUESTION: Where is the value coming from? 问题:价值从何而来? There is no global variable r outside of the javascript function. 在javascript函数之外没有全局变量r From the looks of it, the value would appear to be null . 从外观上看,该值似乎为null Am I missing something here? 我在这里想念什么吗?

ORIGINAL SOURCE OF THE CODE: http://tutorialzine.com/2010/10/ajax-web-chat-css-jquery/ 代码的原始来源: http : //tutorialzine.com/2010/10/ajax-web-chat-c​​ss-jquery/

r is the ajax response object, given to the callback function by tzPOST . r是ajax响应对象,由tzPOST提供给回调函数。 This tzPOST is just a wrapper for $.post . 这个tzPOST只是$.post的包装。 So r is the ajax response by the requested webserver. 所以r是所请求的Web服务器的ajax响应。 Some shorter version of the code for better example: 一些较短版本的代码,例如:

//                                                   | here is 'r'
//                                                   | as parameter of the callback
//                                                   |
$.tzPOST('submitChat', $(this).serialize(), function(r) {
    params['id'] = r.insertID;
});

If you search for the tzPOST function in the scripts.js file, you will see, that it just uses jQuery's $.post function. 如果在scripts.js文件中搜索tzPOST函数,您将看到它仅使用jQuery的$.post函数。 The developer uses this to have a shorthand and a central point for the requests URL: 开发人员使用它来为请求URL提供快捷方式和中心点:

$.tzPOST = function(action, data, callback) {
    $.post('php/ajax.php?action='+action, data, callback, 'json');
}

On the php/server side the response is set in ajax.php by the lines: 在php / server端,通过以下行在ajax.php设置响应:

$response = Chat::submitChat($_POST['chatText']);
echo json_encode($response);

The Chat::submitChat function is found in the Chat.class.php file. Chat.class.php文件中可以找到Chat::submitChat函数。 It inserts everything into database and returns the row id of the database. 它将所有内容插入数据库并返回数据库的行ID。

public static function submitChat($chatText){
    /* someother code here*/

    // save to database and get row id back
    $insertID = $chat->save()->insert_id;

    // return the insert id
    return array(
        'status' => 1,
        // this is later 'r.insertID'
        'insertID' => $insertID
    );
}

Now params['id'] has the value of r.insertID , witch is the databse row id of the inserted data . 现在params['id']的值为r.insertIDwitch是插入数据的数据库行ID


To understand where r comes from, we take a look at the $.post inside the tzPOST function. 为了了解r来源,我们来看看tzPOST函数中的$.post This is a alias for jQuery's $.ajax function. 这是jQuery $.ajax函数的别名。 So instead of $.post we can use $.ajax too. 因此,我们也可以使用$.ajax代替$.post

Then it should be more clear where r comes from. 那么应该更清楚r来源。

// this is exactly the same as the one line used in 'tzPOST'
$.ajax({
    url: 'php/ajax.php?action=' + action,
    data: data,
    dataType: 'json',

    // this is a anonymous callback function
    // it gets griggered whenever the request was successfull
    //
    // the $.ajax will pass the reponse to the anonymous functions first
    // parameter, witch internally is named 'r' now
    success: function(r) {
        // so 'r' is the webservers response
    }
});

So r is only a internal name, passed by the ajax request. 所以r只是一个内部名称,由ajax请求传递。 You can easily rename r : 您可以轻松地重命名r

//                                                   | now the name is 'foo'
//                                                   |
$.tzPOST('submitChat', $(this).serialize(), function(foo) {
    params['id'] = foo.insertID;
});

As I have already commented, r is the response object. 正如我已经说过的, r是响应对象。

tzPost code tz邮政编码

$.tzPOST = function(action, data, callback) {
  $.post('php/ajax.php?action=' + action, data, callback, 'json');
}

It internally calls $.post and if you see $.post 它在内部调用$.post ,如果您看到$.post

$.post("ajax/test.html", function(data) {
  $(".result").html(data);
});

it returns response of the API call. 它返回API调用的响应。

So in my understanding, he is calling post to save user and create a unique ID which is returned as a response of .post in r and is set to params['id'] 因此,以我的理解,他正在调用post来保存用户并创建一个唯一的ID,该ID作为r.post的响应返回,并设置为params['id']

Reference 参考

params['id'] = r.insertID;

is the same as 是相同的

params.id = r.insertID;

r is the input parameter from the callback function of the post. r是帖子的回调函数的输入参数。 It is the data that is sent back from the server. 是从服务器发回的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM