jeudi 13 novembre 2014

flickrapi (js) multiple async calls in a loop


Vote count:

0




I allmost banged my head into the wall because I can't get the following code too work. I'm trying to code a photo gallery with the flickrApi and have problems with multiple async calls. But perhaps there is a cleaner solution to code this.


openPhotoset() is called when clicking the link of a photoset. Unfortunately getting the description of a photo I need to use a different method, which means another async call. I'm looping through the data, but because I make the call in a loop (that's when I have the photo-id available) the deferred of openPhotoset() doesn't resolve after looping but before. I read and have seen examples of $.when() used in a loop, filling an array with deferreds and checking with $.when but I seem to fail horribly at it. Is this the solution I need or is there another road to salvation? ;)


I want to execute different functions after all calls within openPhotoset() has completed.



function openPhotoset(photosetId) {
var currentPhotoset = [],
deferred = $.Deferred();

_requestPhotosOfSet(photosetId).done(function(data){
$(data.photoset.photo).each(function(i, photo){
var objPhoto = {};

objPhoto.id = photo.id;
objPhoto.title = photo.title;
objPhoto.farm = photo.farm;
objPhoto.server = photo.server;
objPhoto.secret = photo.secret;

// get photo description
requestPhotoInfo(photo.id).done(function(data) {

objPhoto.description = data.photo.description._content;
currentPhotoset.push(objPhoto);

}).then(function() {
// TODO: renders with each iteration, shouldnt!
var template = $('#li-gallery').html(),
result = Mustache.render(template, {currentPhotoset:currentPhotoset});

showGallery();
_$fyGallery.find('.gallery-list').html(result);

deferred.resolve();

});
});

});

return deferred;

}


asked 2 mins ago







flickrapi (js) multiple async calls in a loop

Aucun commentaire:

Enregistrer un commentaire