I am using Mustache.js to format a response from a jQuery getJSON() request. The getJSON request gets a list of images names. I want to display a series of these images at the end of my existing content when the call is made.
The JSON that is returned looks like this:
[
{"id":{"_time":1351063373,"_machine":56912161,"_inc":1690583039,"_new":false},"url":"5.jpg","tags":[]},
{"id":{"_time":1351063237,"_machine":56912161,"_inc":1690583038,"_new":false},"url":"Repinzle-Logo.png", "tags":[]},
{"id":{"_time":1351063186,"_machine":56912161,"_inc":1690583037,"_new":false},"url":"21.jpg","tags":[]}}
]
I am parsing it with the each function ... my AJAX request (with jQuery looks like this):
<script type="text/javascript">
var imageNum=20;
var imageCount=0;
function getMoreImages(){
imageCount=imageCount+imageNum;
$.getJSON("/getImages", { start: imageNum, count: imageCount },
function(data){
$.each(data, function(key, val) {
// do stuff with val.url
var url = val.url;
var template = $('#item-tpl').html();
var newitem = Mustache.to_html(template, url);
$('#main').append(newitem);
});
});
}
</script>
And here is the mustache.js template
<script id="item-tpl" type="text/html">
<div><img src="https://myurlstem.com/mydir/{{url}}" class="item"></div>
</script>
As far as I can see I've set everything up correctly, but for some reason url is not being sent to the template correctly.