Skip to content

Commit

Permalink
fix for window event listeners persisted by turbolinks (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Jul 9, 2018
1 parent d759f91 commit bb0060f
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/pagy/extras/javascripts/pagy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

function Pagy(){}

Pagy.windowListeners = [];

Pagy.addInputEventListeners = function(input, handler){
// select the content on click: easier for typing a number
input.addEventListener('click', function(){ this.select() });
Expand Down Expand Up @@ -58,13 +60,16 @@ Pagy.responsive = function(id, tags, widths, series){
pagyBox.insertAdjacentHTML('beforeend', html);
lastWidth = width;
}
};
if (window.attachEvent) { window.attachEvent('onresize', render) }
else if (window.addEventListener) { window.addEventListener('resize', render, true) }
}.bind(this);
window.addEventListener('resize', render, true);
Pagy.windowListeners.push(render);
render();
};

Pagy.init = function(){
// we need to explicitly remove the window listeners because turbolinks persists the window object
Pagy.windowListeners.forEach(function(l){window.removeEventListener('resize', l, true)});
Pagy.windowListeners = [];
['compact', 'items', 'responsive'].forEach(function(name){
var json = document.getElementsByClassName("pagy-"+name+"-json");
for (var i = 0, len = json.length; i < len; i++) {
Expand Down

0 comments on commit bb0060f

Please sign in to comment.