Last active
September 26, 2015 07:07
-
-
Save ravikiranj/1058871 to your computer and use it in GitHub Desktop.
Checking the condition and initiating an AJAX update followed by page update
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
| function handleScroll(){ | |
| var self = this; | |
| var scrollPos; | |
| if(this.updateInitiated){ | |
| return; | |
| } | |
| //Find the pageHeight and clientHeight(the no. of pixels to scroll to make the scrollbar reach max pos) | |
| var pageHeight = document.documentElement.scrollHeight; | |
| var clientHeight = document.documentElement.clientHeight; | |
| //Handle scroll position in case of IE differently | |
| if(Y.UA.ie){ | |
| scrollPos = document.documentElement.scrollTop; | |
| }else{ | |
| scrollPos = window.pageYOffset; | |
| } | |
| //Check if scroll bar position is just 50px above the max, if yes, initiate an update | |
| if(pageHeight - (scrollPos + clientHeight) < 50 && this.retries < this.maxRetries && !this.allStoriesFetched){ | |
| this.updateInitiated = true; | |
| var offset = Y.all(".stream-container .stream-items .stream-item").size(); | |
| //Stop updating once 200 items are reached | |
| if(parseInt(offset, 10) >= 200){ | |
| Y.one("#maxitems").setStyle('display', 'block'); | |
| return; | |
| } | |
| //Show loading gif | |
| Y.one("#loading-gif").setStyle("display", "block"); | |
| document.documentElement.scrollTop += 60; | |
| //var url = "http://ravikiranj.net/drupal/sites/all/hacks/infinite-scroll/infinite_scroll.php?offset="+this.offset; | |
| var url = "http://localhost/infinite-scroll.php?offset="+this.offset; | |
| var oConn = Y.io(url, { | |
| on:{ | |
| success: function(id, o, args){ | |
| //Update pagination unit | |
| args.self.offset += 15; | |
| args.self.retries = 0; | |
| var resp = o.responseText; | |
| var regex = /No more top stories/; | |
| if(resp.match(regex)){ | |
| args.self.allStoriesFetched = true; | |
| Y.one("#no_more_rss").setStyle('display', 'block'); | |
| }else{ | |
| var list = Y.one(".stream-container .stream-items"); | |
| list.set('innerHTML', list.get('innerHTML')+resp); | |
| } | |
| Y.one("#loading-gif").setStyle("display", "none"); | |
| }, | |
| failure: function(id, o, args){ | |
| args.self.retries += 1; | |
| Y.one("#loading-gif").setStyle("display", "none"); | |
| alert('Failed to get data from YQL :('); | |
| }, | |
| complete: function(id, o, args){ | |
| args.self.updateInitiated = false; | |
| } | |
| }, | |
| arguments: { | |
| self: this | |
| } | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment