Created
January 23, 2013 08:59
-
-
Save heralight/4603359 to your computer and use it in GitHub Desktop.
jquery.fileupload completed callback and add image as list with template.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#fileupload').fileupload('option', { | |
......... | |
/* on upload complete event*/ | |
completed:function(e, responseJSON){ | |
var files = getFilesFromResponse(responseJSON); | |
var file = files[0] || | |
{error: 'Empty file upload result'}; | |
if (!file.error) { | |
var data = {}; | |
data.id = name; | |
/* fill data with what you want */ | |
data.fileName = file.name; | |
insertListItems(ulImgContainer, data); | |
console.log(file.name); | |
} | |
} , | |
............. | |
}); | |
var ulImgContainer = $("#img-container-ul"); | |
var itemRender = $("#itemTemplate"); | |
/* apply data to jsrender template and add element */ | |
function insertListItems(el, data) { | |
var irendered = itemRender.render(data); | |
var emptyImg = $("li.add-img-r").first(); | |
if (emptyImg.length) { | |
emptyImg.replaceWith(irendered); | |
} | |
else { | |
el.prepend(irendered); | |
} | |
} | |
function getFilesFromResponse(data) { | |
if (data.result && $.isArray(data.result.files)) { | |
return data.result.files; | |
} | |
return []; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script id="itemTemplate" type="text/x-jsrender"> | |
<li id="{{>fileName}}"> | |
<div class="thumbnail" > | |
<div class="image-style"> | |
<img src="/subfiles/serving/{{>fileName}}"> | |
</div> | |
</div> | |
</li> | |
</script> | |
......... | |
<div id="img-container"> | |
<ul id="img-container-ul" class="thumbnails"> | |
............. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment