Created
April 12, 2016 20:18
-
-
Save ahirschberg/b8c28c77a5445861ccf7ec2999b15167 to your computer and use it in GitHub Desktop.
Example of endless scrolling via ajax. Scroll event with underscore.js debounce
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
/* | |
* Use ajax to live-refresh issues/issues/ when the filters are updated | |
*/ | |
$(function () { | |
if (window.currentPage && window.currentPage == 'issue_list') { | |
/* | |
* sends ajax request via GET for a given query at the current url. | |
* callbacks: fail, done | |
*/ | |
var get_page_num_from_query = function () { | |
var match = /page=(\d+)(?:(?!page=\d+).)*$/; | |
var results = match.exec(window.location.search); | |
return results ? parseInt(results[1], 10) : 1; | |
} | |
var issues_ajax_GET = function (query, callbacks) { | |
$('.issues-table').addClass('loading'); | |
$.ajax({ | |
url: window.location.pathname, | |
data: query, | |
dataType: 'html', | |
method: 'GET', | |
cache: false | |
}).done(function(data) { | |
// if browser supports it, update browser's url with query string | |
if (window.history) { | |
window.history.replaceState(null, "", | |
window.location.pathname + '?' + query); | |
} | |
callbacks.done && callbacks.done(data); | |
$('.issues-table').removeClass('loading'); | |
}).fail(function (err) { | |
console.error('Error loading query:', err); | |
callbacks.fail && callbacks.fail(err); | |
$('.issues-table').removeClass('loading'); | |
$('.issues-table').addClass('error'); | |
}); | |
} | |
var form_data = $('form.filter-form').serialize(); | |
var curr_page = get_page_num_from_query(); | |
console.log('curr page is ', curr_page); | |
$('.filter-form input, .filter-form select').change(function (e) { | |
form_data = $('form.filter-form').serialize(); | |
curr_page = 1; | |
issues_ajax_GET(form_data, { | |
done: function (data) { | |
$('.issues-table tbody').html(data); | |
} | |
}); | |
}); | |
$(window).scroll(_.debounce(function (event) { | |
var element = document.body; | |
if (element.scrollHeight - element.scrollTop < element.clientHeight + 10) { | |
issues_ajax_GET(form_data + "&page=" + (++curr_page), { | |
done: function (data) { | |
$('.issues-table tbody').append(data); | |
} | |
}); | |
} | |
}, 150, false)); | |
} | |
}); | |
/* | |
* Methods taken from underscore.js | |
*/ | |
var _ = {} | |
_.debounce = function(func, wait, immediate) { | |
var timeout, args, context, timestamp, result; | |
var later = function() { | |
var last = _.now() - timestamp; | |
if (last < wait && last >= 0) { | |
timeout = setTimeout(later, wait - last); | |
} else { | |
timeout = null; | |
if (!immediate) { | |
result = func.apply(context, args); | |
if (!timeout) context = args = null; | |
} | |
} | |
}; | |
return function() { | |
context = this; | |
args = arguments; | |
timestamp = _.now(); | |
var callNow = immediate && !timeout; | |
if (!timeout) timeout = setTimeout(later, wait); | |
if (callNow) { | |
result = func.apply(context, args); | |
context = args = null; | |
} | |
return result; | |
}; | |
} | |
_.now = Date.now || function() { | |
return new Date().getTime(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment