Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shopifypartners/a9cc4968be5b4471ea8e1107b09c9884 to your computer and use it in GitHub Desktop.
Save shopifypartners/a9cc4968be5b4471ea8e1107b09c9884 to your computer and use it in GitHub Desktop.
(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