Wilhelm
Wilhelm

Reputation: 1437

Why does $.each show only one of my Mustache templates?

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

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.

Can someone tell me what I'm doing wrong?

Upvotes: 0

Views: 97

Answers (1)

Vasiliy vvscode Vanchuk
Vasiliy vvscode Vanchuk

Reputation: 7169

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

I replace .html() with .append(

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

Upvotes: 2

Related Questions