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 | |
} | |
} | |
} |
I was looking for the same thing, but I think it is in this blog
http://figg-blog.tumblr.com/post/32733177516/infinite-scrolling-knocked-out
How can I detect that scrolling has stopped and then call the loadFunc() ?
How can I defer the scroll event so it doesn't fire so much?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks great!
Do you have an example using this binding?