Skip to content

Instantly share code, notes, and snippets.

@rickydazla
Created December 21, 2011 19:45
Show Gist options
  • Save rickydazla/1507382 to your computer and use it in GitHub Desktop.
Save rickydazla/1507382 to your computer and use it in GitHub Desktop.
Shopify View All (wtf? seriously!)
/*------------------------View all button script----------------------------------------------*/
var _PageUrls = [];
var _Index = 0;
function loadUrlInAjax(url,requestType,dataType,functionToBeExecutedBefore,functionToBeExecutedAfter){
$.ajax({
type: requestType,
url: url,
beforeSend: function(){
//functionToBeExecutedBefore();
},
success: function(data){
functionToBeExecutedAfter(data);
},
dataType: dataType,
});
}
var beforeSendFunction = function(){
}
var displayData = function(data){
var filteredData = $(data).find(".collection");
filteredData.insertBefore( $("#product-list-foot") );
$('li.more').each(function(){
$(this).remove();
});
_Index ++;
//Load Next page if available
if(_Index < _PageUrls.length){
initUrlLoader(_PageUrls[_Index]);
}
}
$('.view-all').click(function(){
loadAllUrls();
var url = _PageUrls[_Index];
initUrlLoader(url);
$('#pagination').remove();
});
function initUrlLoader(url){
var requestType = 'GET';
var dataType = 'html';
loadUrlInAjax(url,requestType,dataType,beforeSendFunction,displayData);
}
function loadAllUrls(){
var currentPage = getQueryVariable('page');
var maxPageNumber = 2;
var maxPageUrl = '';
$('#pagination a').each(function(){
var currentValue = parseInt($(this).text());
if(currentValue > 0 && currentValue > maxPageNumber){
maxPageNumber = currentValue;
maxPageUrl = $(this).attr('href');
}
});
var urlFormat = maxPageUrl.substring(0,maxPageUrl.indexOf("=")+1);
var j=0;
for(var i=1;i<maxPageNumber + 1;i++){
if(currentPage != i){
_PageUrls[j] = urlFormat + i;
j++;
}
}
}
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
return 1;
}
/*------------------------End view all button script----------------------------------------------*/
$(document).ready(function () {
attachClickEvent();
});
@rickydazla
Copy link
Author

@palak
Copy link

palak commented Nov 16, 2016

getting error on attachClickEvent(). Can you please help how to resolve it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment