Forked from gmassel/gist:6e079a47546d88518c71
Last active
September 25, 2020 20:44
-
-
Save shopifypartners/a9cc4968be5b4471ea8e1107b09c9884 to your computer and use it in GitHub Desktop.
Using JavaScript on client stores, server request JavaScript - https://www.shopify.com/partners/blog/28500611-using-javascript-to-super-power-your-clients-shopify-site
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