Skip to content
Browse files

Added custom events for file upload completion and upload errors so t…

…hat application-specific code can listen for these events and handle server responses and errors.
  • Loading branch information...
1 parent a8f56d5 commit 92acdb1e2ad73235c0500373d7e8aa6339de2803 @justindarc justindarc committed Mar 21, 2012
Showing with 27 additions and 3 deletions.
  1. +8 −0 index.html
  2. +19 −3 uploadkit.js
View
8 index.html
@@ -8,6 +8,14 @@
<script type="text/javascript" src="jquery-1.7.1.js"></script>
<script type="text/javascript" src="externals/plupload/js/plupload.full.js"></script>
<script type="text/javascript" src="uploadkit.js"></script>
+
+ <script type="text/javascript">
+ $(function() {
+ $('.uk-input').bind(UKEventType.FileUploaded, function(evt) {
+ console.log(evt.response);
+ });
+ });
+ </script>
</head>
<body>
<form method="post" action="#">
View
22 uploadkit.js
@@ -1,3 +1,8 @@
+var UKEventType = {
+ FileUploaded: 'UKFileUploaded',
+ UploadError: 'UKUploadError'
+};
+
var UploadKit = function(input) {
if (!window['plupload']) {
console.error('Unable to initialize UploadKit; Plupload dependency not found');
@@ -10,7 +15,7 @@ var UploadKit = function(input) {
var id = (Date['now']) ? Date.now() : +new Date(); // TODO: Verify this failover works in IE.
var baseUrl = '';
$('script').each(function(index, element) {
- var src = $(element).attr('src');
+ var src = $(element).attr('src') || '';
var endIndex = src.indexOf('uploadkit.js');
if (endIndex !== -1) baseUrl = (endIndex === 0) ? './' : src.substring(0, endIndex);
});
@@ -22,7 +27,7 @@ var UploadKit = function(input) {
var $element = this.$element = $input.wrap('<div id="uk-container-' + id + '" class="uk-container span6"/>').parent();
$element.data('uploadKit', this);
- $input.remove();
+ $input.hide();
var infoHtml = (isMultiple) ?
'<h1>No Files Selected</h1><h2>Browse for files to upload or drag and drop them here</h2>' :
@@ -112,12 +117,18 @@ var UploadKit = function(input) {
$bar.css('width', file.percent + '%');
});
- uploader.bind('FileUploaded', function(uploader, file) {
+ uploader.bind('FileUploaded', function(uploader, file, response) {
var $tr = $tbody.find('#' + file.id);
var $progress = $tr.find('.progress');
var $bar = $progress.find('.bar');
$progress.removeClass('progress-info active').addClass('progress-success');
$bar.html('Done');
+
+ $input.trigger($.Event(UKEventType.FileUploaded, {
+ uploader: uploader,
+ file: file,
+ response: response
+ }));
});
uploader.bind('Error', function(uploader, error) {
@@ -135,6 +146,11 @@ var UploadKit = function(input) {
}
$td.html(message);
+
+ $input.trigger($.Event(UKEventType.UploadError, {
+ uploader: uploader,
+ error: error
+ }));
});
$tbody.delegate('a.close', 'click', function(evt) {

0 comments on commit 92acdb1

Please sign in to comment.
Something went wrong with that request. Please try again.