Created
September 11, 2012 21:27
-
-
Save diego-carrera/3702201 to your computer and use it in GitHub Desktop.
JQuery Deferred Post Example
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
var postImages = function() { | |
clearInterval(camInterval); | |
var tasks = []; | |
var done = 0; | |
$('#ImageDataContainer textarea').each(function(){ | |
var imgData = $(this).val(); | |
tasks.push(postImage(imgData)); | |
}); | |
var total = tasks.length; | |
$.when.apply($, tasks) | |
.then(function(){ | |
fotosPublished = true; | |
$('#fb-publish-modal').modal('hide'); | |
$('#fb-publish-modal').on('hidden', function(){ | |
$('#ImageCreateForm').submit(); | |
}); | |
}) | |
.progress(function(data){ | |
done++; | |
var per = ( done / total ) * 100; | |
$('#fb-progress-message').html('Publicando imagen ' + done + ' de ' + total); | |
$('#fb-progress-bar').css('width', per+'%'); | |
}) | |
.fail(function(){ | |
}); | |
}; | |
var postImage = function(imageData) { | |
return $.Deferred(function(dfd){ | |
$.post('/images/sendToFacebook', {'data':{'Image':imageData}}) | |
.success(function(data){ | |
if ( typeof data == 'object' && !('error' in data)) { | |
dfd.notify(data); | |
dfd.resolve(data); | |
} else { | |
dfd.notify(data); | |
dfd.reject(data); | |
} | |
}) | |
.error(function(){ | |
dfd.reject('ajax error'); | |
}); | |
}).promise(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment