简体   繁体   English

jQuery-模板的自定义HTML-奇怪的行为

[英]Jquery - custom HTML for template - strange behavior

var $template;
$.get('/templates/searchTemplate.tmpl', function(HTMLres) {
    $template = $(HTMLres);
});
$.each(data.results, function(key, value) {
    $template.find('button').off().on('click', function() { console.log('you clicked me') });
    $template.find('.media-body > strong').html(value.firstname + ' ' + value.lastname);
    $template.find('.email').html(value.mail);
    $template.find('.phone').html(value.phone);
    $template.find('.age').html(value.age);
    $('#searchRsults').append($template);
});

When I try to use this code instead of having the template file to be Appended to the UL element, this will clear the previous append and append the next result to the UL element. 当我尝试使用此代码而不是将模板文件追加到UL元素时,这将清除上一个追加并将下一个结果追加到UL元素。

The template file is just a -> <LI> </LI> with some spans. 模板文件只是一个-> <LI> </LI>带有一些跨度。

Note that if I put the | 请注意,如果我将| $.get (template) in the $.each part of the code everything works like it supposed to be all the <LI> </LI> element are appended one after another. 在代码的$.each部分中的$.get (template)中,一切工作都像应该是所有<LI> </LI>元素一样,被一个接一个地追加。

But doing so it makes a lot of request for the template file. 但是这样做会导致对模板文件的大量请求。 On every iteration actually. 实际上在每次迭代中。

Even if I use the .DONE after the Ajax request still the result is the same the <LI> </LI> gets overwritten not appended 即使我在Ajax请求之后使用.DONE ,结果仍然相同<LI> </LI>被覆盖而不附加

You should put the each() inside the success callback so the $template will be available, else you're trying to use the $template when it's not returned yet from the asynchronous HTTP (Ajax) request : 您应该将each()放在成功回调中,以便$template可用,否则当您尚未从异步HTTP(Ajax)请求返回它时,您尝试使用$template

var $template;
$.get('/templates/searchTemplate.tmpl', function(HTMLres) {
    $template = $(HTMLres);

    $.each(data.results, function(key, value) {
        $template.find('button').off().on('click', function() { console.log('you clicked me') });
        $template.find('.media-body > strong').html(value.firstname + ' ' + value.lastname);
        $template.find('.email').html(value.mail);
        $template.find('.phone').html(value.phone);
        $template.find('.age').html(value.age);
        $('#searchRsults').append($template);
    });
});

Take a look to How do I return the response from an asynchronous call? 看一看如何从异步调用返回响应? .

Hope this helps. 希望这可以帮助。

function getResults(searchTerm){
           return $.get("/api/search", { 'searchFor': keyword });
        }

function getTemplate(){
            return $.get('/templates/searchTemplate.tmpl')
        }

$.when(getResults(keyword), getTemplate())
            .done(function(result1, result2){
                $('ul#searchRsults').empty();
                if (result1[0].results.indexOf('no results') != 0) {
                    $.each(result1[0].results, function(key, value) {

                        var $template = $(result2[0]);
                            $template.find('.img-avatar').html(img);
                            $template.find('button').off().on('click', function() { console.log('you clicked me') });
                            $template.find('.media-body > strong').html(value.firstname + ' ' + value.lastname);
                            $template.find('.email').html(value.mail);
                            $template.find('.phone').html(value.phone);
                            $template.find('.age').html(value.age);
                            $('#searchRsults').append($template);
                    });
            }
        })

after some read - I ended with this solution, in chrome console the request for template is made only once. 经过一番阅读-我以这种解决方案结束了,在chrome控制台中,对模板的请求仅进行了一次。 Hope this helps sombady 希望这对尚巴迪有帮助

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

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