为什么$ .each只显示我的一个Mustache模板?

时间:2022-09-18 14:32:01

I'm using $.get inside $.each to call an external Mustache template to use on each Photo object in my array.

我在$ .each中使用$ .get来调用外部的Mustache模板,以便在我的数组中的每个Photo对象上使用。

Here is my code:

这是我的代码:

$.when.apply($, requests).then(function(dataOne, dataTwo) {
    $.each(dataOne, function(idx, obj) {
        $.get("myTemplate.mst", function(template) {
            var rendered = Mustache.render(template, obj);

            $photoElem.html(rendered);
        });
    });
});

My problem is that once I refresh the screen, only one of my array objects shows up. I'm aware of {{#item}} to iterate through the array but I'm using $.each for a very specific reason.

我的问题是,一旦刷新屏幕,我的数组对象中只有一个出现。我知道{{#item}}要遍历数组但是我使用$ .each是出于一个非常具体的原因。

Can someone tell me what I'm doing wrong?

有人能告诉我我做错了什么吗?

1 个解决方案

#1


2  

try this one

试试这个

 $.when.apply($, requests).then(function(dataOne, dataTwo) {
      $.get("myTemplate.mst", function(template) {
          $.each(dataOne, function(idx, obj) {
             var rendered = Mustache.render(template, obj);
             $photoElem.append(rendered);
         });
     }); 
 });

Suppose problem causes you replace content of $photoElem ( using .html() ) instead of appending each new item

假设问题导致您替换$ photoElem的内容(使用.html())而不是附加每个新项目

I replace .html() with .append(

我用.append替换.html()(

And change code to read template only once ( as @Andy say )

并将代码更改为仅读取一次模板(如@Andy所说)

#1


2  

try this one

试试这个

 $.when.apply($, requests).then(function(dataOne, dataTwo) {
      $.get("myTemplate.mst", function(template) {
          $.each(dataOne, function(idx, obj) {
             var rendered = Mustache.render(template, obj);
             $photoElem.append(rendered);
         });
     }); 
 });

Suppose problem causes you replace content of $photoElem ( using .html() ) instead of appending each new item

假设问题导致您替换$ photoElem的内容(使用.html())而不是附加每个新项目

I replace .html() with .append(

我用.append替换.html()(

And change code to read template only once ( as @Andy say )

并将代码更改为仅读取一次模板(如@Andy所说)