Search code examples
jqueryhandlebars.jsjquery-file-upload

jQuery File Upload Handlebars Templates Disappearing


I am using handlebars.js as my template engine with jQuery File Upload and when I upload a file it flashes on the screen then disappears completely its not even hidden in the dom.

My JavaScript:

$('#photo-uploader').fileupload({
    acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
    autoUpload: true,
    maxFileSize: 5000000,
    url: '/member/property/ajaxUpload',
    filesContainer: $('.files'),
    uploadTemplateId: null,
    uploadTemplate: function(o) {
        var rows = $();
        $.each(o.files, function(index, file) {
            var source = $('#photo-upload').html();
            var template = Handlebars.compile(source);
            var data = {o: o, file: file};
            var template = template(data);

            var row = $(template);

            rows = rows.add(row);
        });

        return rows;
    }
});

Handlebars Template:

<script id="photo-upload" type="text/x-handlebars-template">
    <li class="template-upload upload-thumbs img-thumb">
        <span class="preview"></span>
        <a href="#" class="delete-image">Delete</a>
        <a href="#" class="view-image">View</a>
        <h3>Title <em>Click to edit</em></h3>
    </li>
</script>

If I replace the uploadTemplate method with the one in the example it works, the only thing different in the object is the tag.

https://github.com/blueimp/jQuery-File-Upload/wiki/Template-Engine


Solution

  • Add the fade class to your <li>, as jQuery File Upload uses Bootstrap to show / hide elements