Hitscotty
6/8/2018 - 4:43 PM

authoring clientside api's


/*https://www.shopify.com/partners/blog/28500611-using-javascript-to-super-power-your-clients-shopify-site*/
(function(window, $){
	if(!$){
    throw new Error('$ is not defined')
  }	
  
  
  // private fields
  let requestLimit = 50;
  let collections = [];
  let collectionsHandleMap = [];
  let products = [];
  let productsHandleMap = [];
 
   
  // public static fields
  
 	// snow joe api 
 	let api = {

 	}
 
  api.init = function(){}
    
  
  api.getProductsFor = function getProducts(collectionHandle,productHandle){
 
    return $.get(`/collections/${collectionHandle}?view=pagination-endpoint`, function(response){
      response = JSON.parse(response);
			
      for (let i = 0; i < response.pages; i++) {
        let pageNumber = 1 + i;
        let $productRequest = $.get(`/collections/${collectionHandle}?view=products-endpoint&page=${pageNumber}`, function(response){
          response = JSON.parse(response);
          products.push(...response.products);
          return response
        });
       }
    })
  }
 
  api.getProducts = function(){
    return products;
  }
  
	window.sj = api;
 
})(window, jQuery)