Created
May 20, 2015 19:33
-
-
Save gmassel/6e079a47546d88518c71 to your computer and use it in GitHub Desktop.
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
(function(Resources, $, undefined) { | |
// Private | |
var requestLimit = 50; | |
var collections = []; | |
var collectionsHandleMap = []; | |
var products = []; | |
var productsHandleMap = []; | |
var getResources = function() { | |
var $getCollections = function(totalCollections) { | |
var collectionDeferreds = []; | |
for (var i = 0; i < totalCollections/requestLimit; i++) { | |
var pageNumber = 1 + i; | |
var $collectionRequest = $.get('/collections?view=collections-endpoint&page='+pageNumber, function(response) { | |
response = JSON.parse(response); | |
$.merge(collections, response.collections); | |
}); | |
collectionDeferreds.push($collectionRequest); | |
} | |
return $.when.apply($, collectionDeferreds).done(function() { | |
collectionsHandleMap = $.map(collections, function(collection, index) { | |
return collection.handle; | |
}); | |
}); | |
}; | |
var $getProducts = function(totalProducts) { | |
var productDeferreds = []; | |
for (var i = 0; i < totalProducts/requestLimit; i++) { | |
var pageNumber = 1 + i; | |
var $productRequest = $.get('/collections/all?view=products-endpoint&page='+pageNumber, function(response) { | |
response = JSON.parse(response); | |
$.merge(products, response.products); | |
}); | |
productDeferreds.push($productRequest); | |
} | |
return $.when.apply($, productDeferreds).done(function() { | |
productsHandleMap = $.map(products, function(product, index) { | |
return product.handle; | |
}); | |
}); | |
}; | |
var $getActive = $.get('/collections/all?view=pagination-endpoint', function(response) { | |
response = JSON.parse(response); | |
$.when($getCollections(response.totalCollections), $getProducts(response.totalProducts)).done(function() { | |
$.each(products, function(index, product) { | |
$.each(product.collections, function(index, collectionHandle){ | |
collections[collectionsHandleMap.indexOf(collectionHandle)].products.push(product.handle) | |
}); | |
}); | |
console.log(collections); | |
console.log(products); | |
}) | |
}); | |
}; | |
Resources.retrieveProduct = function(handle) { | |
return products[productsHandleMap.indexOf(handle)]; | |
}; | |
Resources.buildCollection = function(collectionHandle) { | |
var builtCollection = []; | |
var collection = collections[collectionsHandleMap.indexOf(collectionHandle)] | |
$.each(collection.products, function(index, handle) { | |
builtCollection.push(products[productsHandleMap.indexOf(handle)]); | |
}); | |
return builtCollection; | |
}; | |
Resources.init = function() { | |
getResources(); | |
}; | |
}(window.Resources = window.Resources || {}, jQuery)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment