Created
October 2, 2012 11:27
-
-
Save fredkingham/3818315 to your computer and use it in GitHub Desktop.
knockout infinite scrolling
This file contains hidden or 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
ko.bindingHandlers.scroll = { | |
updating: true, | |
init: function(element, valueAccessor, allBindingsAccessor) { | |
var self = this | |
self.updating = true; | |
ko.utils.domNodeDisposal.addDisposeCallback(element, function() { | |
$(window).off("scroll.ko.scrollHandler") | |
self.updating = false | |
}); | |
}, | |
update: function(element, valueAccessor, allBindingsAccessor){ | |
var props = allBindingsAccessor().scrollOptions | |
var offset = props.offset ? props.offset : "0" | |
var loadFunc = props.loadFunc | |
var load = ko.utils.unwrapObservable(valueAccessor()); | |
var self = this; | |
if(load){ | |
element.style.display = ""; | |
$(window).on("scroll.ko.scrollHandler", function(){ | |
if(($(document).height() - offset <= $(window).height() + $(window).scrollTop())){ | |
if(self.updating){ | |
loadFunc() | |
self.updating = false; | |
} | |
} | |
else{ | |
self.updating = true; | |
} | |
}); | |
} | |
else{ | |
element.style.display = "none"; | |
$(window).off("scroll.ko.scrollHandler") | |
self.updating = false | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can I defer the scroll event so it doesn't fire so much?