Throughout my entire time working on a v0.2 release I haven’t been able to get the correct choronological order (it should be reverse chronological order) for the new entries that load OnScroll. This is an essential part of the pushlog as it shows users when the entries were added. I’m using a script called json-pushes to acquire the data I need to show. For each call to the script I need to provide a startID and endID. At first I started by calling the script like so:
- startID=0, endID=20
- startID=20, endID=40
- startID=40, endID=60
- startID=60, endID=80
- etc…
This didn’t work since since ID=1 is the very earliest entry in the pushlog when I should be starting with the later entries and working my way down to the earlier entries. So, what I needed to do was somehow acquire the ID of the latest entry in the pushlog. Thus, I wrote this new function in the server side file pushlog-feed.py
1 2 3 4 5 6 7 8 | def getMaxEntries(repo): stmt = 'SELECT id from pushlog' max = 0 conn = sqlite.connect(os.path.join(repo.path, 'pushlog2.db')) for id in conn.execute(stmt): max += 1 return max |
The above code provides me with the maximum ID in the pushlog. Now I need to provide this value to the client side so that I know which entry I should start off with when I make my first call to json-pushes. The following is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | function getMaxEntries() { var entries = new XMLHttpRequest(); var max = 0; entries.open('GET', '/json-pushes?startID=0&endID=5', true); entries.onreadystatechange = function() { if(entries.readyState == 4) { if(entries.status != 404) { var entryData = new Function("return " + entries.responseText) (); max = entryData[1].max; start = max - 20; } } else return 0; } entries.send(null); } |
So it turns out that I needed to make two XMLHttpRequest’s to first acquire the maximum ID (see above) and then the next one to actually acquire all the data to show OnScroll. Thus, now everytime the user reaches the end of the page json-pushes loads the IDs with the following pattern:
- startID=[MAX], endID=[MAX-20]
- startID=[MAX-20], endID=[MAX-40]
- startID=[MAX-40], endID=[MAX-60]
- startID=[MAX-60], endID=[MAX-80]
- etc…