Skip to content

Instantly share code, notes, and snippets.

@gnarf
Forked from fission6/jquery deferred api calls
Created October 31, 2012 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnarf/3989405 to your computer and use it in GitHub Desktop.
Save gnarf/3989405 to your computer and use it in GitHub Desktop.
complex use case for API calls utilizing jQuery deferreds
// outer IIFE - gives us $ === jQuery!, also, makes every var/function inside "private"!
window.MYAPI = (function($) {
function processSiteRequest( data ) {
var sites = data.response.sites;
// loop through each site given back for the paginated result set
$.each( sites || [], function(index, site) {
debugPrint("Site: " + site.name);
$.each( site.content_categories || [], function( index, category ) {
// build up category to publisher map
debugPrint(" Category: " + category.name);
if ( category.id in category_publisher_map ) {
debugPrint("! category in map");
category_publisher_map[category.id].publishers[site.publisher_id] = {publisher_name: site.publisher_name};
}
else {
debugPrint("! category new so inserting as key in map");
publisher_entry = {};
publisher_name = site.publisher_name;
publisher_entry[site.publisher_id] = {publisher_name: publisher_name};
category_publisher_map[category.id] = {
name: category.name,
publishers: publisher_entry
};
}
});
});
}
function collectSites( ) {
var site_def = $.Deferred();
var start_element_list,
requests_to_make,
requests_made;
function SitePaginationRequests( number_of_items ) {
function buildSiteRequests( start_element ) {
var def = $.Deferred();
var query_data = {
start_element: start_element,
num_elements: NUM_ELEMENTS
};
$.getJSON('http://sand.api.whatever.com/site', query_data)
.pipe(processSiteRequest)
.done(function( data ) {
site_def.notify( ++requests_made / requests_to_make );
def.resolve( data );
});
return def.promise();
}
console.log( start_element_list, start_element_list.length );
if ( start_element_list.length ) {
var chunkedRequests = $.map(start_element_list.splice(0,2), buildSiteRequests);
// recursion, is this the right approach?
$.when.apply(null, chunkedRequests).done(SitePaginationRequests);
}
else {
site_def.resolve();
}
}
return $.getJSON('http://sand.api.whatever.com/site').pipe(function(data) {
var number_of_items = data.response.count;
start_element_list = _.range(0, number_of_items, NUM_ELEMENTS);
requests_to_make = start_element_list.length;
requests_made = 0;
SitePaginationRequests( number_of_items );
return site_def.promise();
});
}
function getPublishersWithSelectedCategory( ) {
return $.when( collectSites(), collectPlacements() ).progress(function(sites, placements) {
max_site_progress = findMaxProgressPercent( sites );
max_placement_progress = findMaxProgressPercent( placements );
progress_percent = Math.floor(( max_site_progress + max_placement_progress ) * 100 / 2) + "%";
$("#gathering-progress-bar .bar").width( progress_percent );
}).pipe(function() {
console.log("Done with Service Calls");
return category_publisher_map;
});
}
// close the IIFE, and return our "public interface"
return {
getPublishersWithSelectedCategory: getPublishersWithSelectedCategory
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment