简体   繁体   English

第三次调用后,Ajax执行成功功能的一半

[英]Ajax executes half of the success function after third call

I call ajax to draw a table from the mysql datas and put textareas below that table to be able to write a documentation for each fields. 我调用ajax从mysql数据中绘制一个表,并将textareas放在该表的下面,以便能够为每个字段编写文档。

Here's my problem. 这是我的问题。 After I type the table name to the search field and click the button, I get the whole page as I wished. 在搜索字段中输入表格名称并单击按钮后,我将获得所需的整个页面。 After I search for the same table name, I get the same page again, but after I check the same table the third time, I only get two tables being drew. 搜索相同的表名后,我会再次获得相同的页面, 但是第三次​​检查同一张表后,我只会得到两个表。 The same issue occurs if I check 'test' table once, then 'test2' twice. 如果我一次检查'test'表,然后两次检查'test2' ,则会发生相同的问题。

UPDATE: After the second ajax call (so if you type something and click to check meanwhile an other one is in the browser) the jQuery plugin only load the mysqlTable template and doesn't the textareas. 更新:在第二个ajax调用之后(因此,如果您键入一些内容并单击以同时检查另一个浏览器是否在浏览器中),jQuery插件只会加载mysqlTable模板,而不会加载textareas。 Despite this textareas still appear, but if you call the third time any table then the issue occurs, the success function stops executing half-way and I only see two tables without textareas. 尽管此textareas仍然出现,但是如果您第三次调用任何表,则会出现问题,成功函数将中途停止执行,并且我只会看到两个没有textareas的表。

What I noticed is that the third time it calls ajax, it only executes until that part where I left the comment /* *** this is the point where ... That's the reason why textareas and other does not appear. 我注意到的是,它第三次调用ajax时,它只会执行到我留下注释的那一部分为止/* *** this is the point where ...这就是textareas等不出现的原因。

It supposed to be like this. 应该是这样的。 应该是这样的。

But it looks like this after the third call. 但是在第三次通话后看起来像这样。 但是第三次​​通话后看起来像这样

I disable the search input while loading, in order to avoid multiple ajax call, but something happens here. 为了避免多次ajax调用,我在加载时禁用了搜索输入,但是这里发生了一些事情。

JS ajax call JS ajax调用

function templatesHandling(){  
    clear();
    $('#textareaHolder').html('');
   $.ajax({
        url: "ajax/table.php?function=get_table_data&table="+tableName,
        dataType: "json", // it'll convert json to objects
        type : "GET",
        beforeSend : function(){ $('#ajax-loader-gif').show(); $('#search').attr({'disabled':'disabled','placeholder':'Loading...'});},
        complete : function(){ $('#ajax-loader-gif').hide(); $('#search').removeAttr('disabled').prop('placeholder','Type the table name..'); },
        error: function(er){ $("#check").after("<span id='error'>Error</span>"); },
        success: function(data){ 

        // add the current table name to the first object in order to be able to print out with {{tableName}}       

        if (!data[0].TableName){
            data[0].TableName = tableName; 
        }
           // call the 'table' template and send the data to work with
               $("#tableHolder").loadFromTemplate({
                   template :  "mysqlTable",
                   data : data
               });    

           /* *** this is the point where sometimes the ajax call stops executing *** */       

           // call the 'textareas' template and send the data to work with 
               $("#textareaHolder").loadFromTemplate({
                   template :  "textareas",
                   data : data
               });            

           /* *** parseWiki *** */
           $('#parseWikiHolder').show(); // show the last textarea 
           for (key in data[1]){ 
            res_head[++j] = '!scope="col"| '+ key + '\n'; // 
           }
           for(var i=1;i<data.length;i++){
                for(key in data[i]){
                    res_body[++j] = '|'+data[i][key]+'\n';
                }; // -for in | parse <tbody>
                res_body[++j] = '|-\n';  
           }; // -for | parse <tbody>   
        } // -success
    });
};

I use jQuery HandlebarsJS to load Handlebars templates from separated files. 我使用jQuery HandlebarsJS从分离的文件中加载Handlebars模板。

Do you have any thought about this issue? 您对这个问题有任何想法吗? Why does it happen? 为什么会发生? (let me know if more code needed) (让我知道是否需要更多代码)

UPDATE 更新

I inserted the full JS code of mine to JSFiddle, click here ! 我将我的完整JS代码插入JSFiddle, 请单击此处

textareas (template) 文本区域(模板)

{{#each this}}
    {{#if TableName}}
        <label for='{{TableName}}'>Description of <em class="tableName">{{TableName}}</em> table</label>
        <textarea id='{{TableName}}'></textarea>
    {{/if}}
    {{#if Field}}

        <label for='{{Field}}'>Description of <em class="tableName">{{Field}}</em> field</label>
        <textarea id='{{Field}}'></textarea>

    {{/if}}
{{/each}}

mysqlTable (template) mysqlTable(模板)

<table>
    <thead>
    <tr>
        {{#each this}}
            {{#if TableName}}
                <th class="structure_thead" colspan="6">Structure of {{TableName}} table</th>
            {{/if}} 
        {{/each}}
    </tr>
    <tr>
        <th>Field</th>     
        <th>Type</th> 
        <th>Null</th> 
        <th>Key</th> 
        <th>Default</th> 
        <th>Extra</th> 
    </tr>    
    </thead>
    <tbody> 
    {{#each this}} 
    {{#if Field}}
        <tr> 
            <td><a href="#{{Field}}"><span class="ui-icon ui-icon-link"></span>{{Field}}</a></td>
            <td>{{Type}}</td>
            <td>{{Null}}</td>
            <td>{{Key}}</td>
            <td>{{Default}}</td>
            <td>{{Extra}}</td>  
        </tr>   
    {{/if}}
    {{/each}}
    </tbody>
</table>

HTML 的HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>Doksi</title>
<link rel="stylesheet" type="text/css" href="css/styles.css">
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<link href='http://fonts.googleapis.com/css?family=Vollkorn' rel='stylesheet' type='text/css'>
</head>
<body>

<div class="container">
    <div class="searchHolder">
        <input id="search" type="text" placeholder="Type the table name.."/>
        <input id="check" type="button" value="Check" />
        <img id="ajax-loader-gif" src="img/ajax-loader.gif" />
    </div>
    <div id="tableHolder"></div>
    <div id="textareaHolder"></div>
    <div id="parseWikiHolder">
        <input type="text" disabled="disabled" value="Ready to copy?"/>
        <input id="copy" type="button" value="Hell, yeah!" />
        <textarea id="parseWiki"></textarea>
    </div>
</div>

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.3.0/handlebars.min.js" type="text/javascript" charset="utf-8"></script>
<script src="js/jquery.loadFromTemplate.js"></script>
<script src="js/ajax.js"></script>
</body>
</html>

Get the table name - JSON file I receive 获取表名称-我收到的JSON文件

 [
    {
    table: "teszt"
    },
    {
    table: "teszt2"
    }
 ]

Get the table's data - JSON file I receive 获取表的数据-我收到的JSON文件

 [
    {
    eredmeny: "siker"
    },
    {
    Field: "id",
    Type: "int(11)",
    Null: "NO",
    Key: "PRI",
    Default: null,
    Extra: "auto_increment"
    },
    {
    Field: "name",
    Type: "varchar(64)",
    Null: "NO",
    Key: "",
    Default: null,
    Extra: ""
    },
    {
    Field: "type",
    Type: "enum('y','n')",
    Null: "NO",
    Key: "",
    Default: "n",
    Extra: ""
    },
    {
    Field: "date",
    Type: "datetime",
    Null: "NO",
    Key: "",
    Default: null,
    Extra: ""
    }
    ]

UPDATE 2 更新2

I commented out that jQuery plugin and tried a simple ajax call, but the same issue occurs. 我注释掉了jQuery插件并尝试了一个简单的Ajax调用,但是发生了同样的问题。 If I run this code, just the table will appear. 如果我运行此代码,将仅显示该表。 If I change the code as textareas are in the first place, then only they will appear. 如果我首先将代码更改为textareas,则只会显示它们。 Why does the code stops executing without any error? 为什么代码停止执行而没有任何错误? Are the two ajax call kick each other out? 两个ajax调用是否相互淘汰?

The new ajax calls 新的ajax调用

 (function getTemplateAjax(path) {
        var source;
        var template;

        $.ajax({
            url: path, //ex. js/templates/mytemplate.handlebars
            cache: true,
            success: function(data) {
                source    = data;
                template  = Handlebars.compile(source);
                $('#tableHolder').html(template(o));
            }               
        });         
    })('templates/mysqlTable.html')     

    (function getTemplateAjax2(path) {
        var source;
        var template;

        $.ajax({
            url: path, //ex. js/templates/mytemplate.handlebars
            cache: true,
            success: function(data) {
                source    = data;
                template  = Handlebars.compile(source);
                $('#textareaHolder').html(template(o));
            }               
        });         
    })('templates/textareas.html')    

I found a (half)solution 我找到了一个(半)解决方案

As I copied that new code block to below UPDATE2, after I changed that again and found the (half)solution. 当我将该新代码块复制到UPDATE2下面时,我再次对其进行更改并找到了(一半)解决方案。

...
       success: function(o){ 
        // add the current table name to the first object in order to be able to print out with {{tableName}}       

           if (!o[0].TableName){
                o[0].TableName = tableName;
           };

            // call the 'mysqlTable' template and send the data to work with
            (function getTemplateAjax(objectum, path, element){
                var source, template; 
                $.ajax({
                    url: path,
                    cache: true,
                    success: function(data) {
                        source    = data;
                        template  = Handlebars.compile(source);
                        element.html(template(objectum));
                    }               
                }); 
            })(o, 'templates/mysqlTable.html', $('#tableHolder'));         

            // call the 'textareas' template and send the data to work with     
            (function getTemplateAjax(objectum, path, element){
                var source, template;
                $.ajax({
                    url: path,
                    cache: true,
                    success: function(data) {
                        source    = data;
                        template  = Handlebars.compile(source);
                        element.html(template(objectum));
                    }               
                });          
            })(o, 'templates/textareas.html', $('#textareaHolder'));
...

It's quite ugly and with more template files it won't the best practice, so I'll look for a better solution. 这很丑陋,并且模板文件过多,并不是最佳做法,因此我将寻求更好的解决方案。 At least it works now. 至少现在可以了。

So the bug was probably inside the plugin. 因此,该错误可能在插件内部。 I am going to write my own jQuery plugin for this task. 我将为此任务编写自己的jQuery插件。

I think the problem is with the async mode for the ajax call for that reason I just commited a new changes you can take a look into the examples: 我认为问题在于ajax调用的异步模式,因此我刚刚提交了一个新更改,因此您可以查看示例:
https://github.com/CKGrafico/jQuery_HandlebarsJS/commit/41eb4c7c39f9c1ef8bbd1015b594d0a33bb585ac maybe this could fix your problem. https://github.com/CKGrafico/jQuery_HandlebarsJS/commit/41eb4c7c39f9c1ef8bbd1015b594d0a33bb585ac也许可以解决您的问题。

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

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