Created
April 30, 2014 14:54
-
-
Save beckyconning/42c17bc91e683bbf1e16 to your computer and use it in GitHub Desktop.
This file contains 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
// assume only change can be a new document | |
// also assume ids are sequential | |
var maxRetriesForNewMilkingRecordsCheck = 1; | |
var retriesForNewMilkingRecords = 0; | |
var databaseUrl = 'http://127.0.0.1:8091/milking_records'; | |
// go get the milking records | |
var viewSuffix = '/_all_docs?update_seq=true'; | |
var viewUrl = databaseUrl + viewSuffix; | |
var milkingRecords = { | |
total_rows: 2, | |
offset: 0, | |
update_seq: 2, | |
rows: [ | |
{ | |
id: "1338b6d1a87f2a6666aee1fa70000763", | |
key: "1338b6d1a87f2a6666aee1fa70000763", | |
value: { | |
rev: "1-967a00dff5e02add41819138abb3284d" | |
} | |
}, | |
{ | |
id: "8de57c80d72f528abbadd15899000111", | |
key: "8de57c80d72f528abbadd15899000111", | |
value: { | |
rev: "1-967a00dff5e02add41819138abb3284d" | |
} | |
} | |
] | |
}; | |
var getMilkingRecordsStartingAtId = function(startId) { | |
var viewSuffix = '/_all_docs?update_seq=true&startkey="' + startId + '"'; | |
// make a copy of milking records | |
var previousMilkingRecords = jQuery.extend({}, milkingRecords); | |
// go get these documents | |
// put them into milkingRecords | |
// prepend previously fetched rows to the milkingRecords rows | |
milkingRecords.rows = previousMilkingRecords.rows.concat(milkingRecords.rows); | |
}; | |
var showErrorMessageAndRetryButton = function() { | |
// show error message | |
// show button that lets user call checkForNewMilkingRecords themselves | |
}; | |
var retryCheckForNewMilkingRecords = function() { | |
retriesForNewMilkingRecords = retriesForNewMilkingRecords + 1; | |
if (retriesForNewMilkingRecords < maxRetriesForNewMilkingRecordsCheck) { | |
checkForNewMilkingRecords(); | |
} | |
else { | |
retriesForNewMilkingRecords = 0; | |
showErrorMessageAndRetryButton(); | |
} | |
}; | |
var checkForNewMilkingRecords = function checkForNewMilkingRecords(data) { | |
var changesSuffix = '/_changes?since=' + milkingRecords.update_seq | |
var changesUrl = databaseUrl + changesSuffix; | |
var changesRequest = $.ajax({}); | |
changesRequest.done(checkForNewMilkingRecords); | |
changesRequest.fail(retryCheckForNewMilkingRecords); | |
if (data) { | |
if (data.results) getMilkingRecordsStartingAtId(data.results[0].id); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment