Skip to content

Instantly share code, notes, and snippets.

@kauffmanes
Last active June 18, 2017 14:35
Show Gist options
  • Save kauffmanes/6020bff70b5919f029f9e2ae3e39c757 to your computer and use it in GitHub Desktop.
Save kauffmanes/6020bff70b5919f029f9e2ae3e39c757 to your computer and use it in GitHub Desktop.
save function
//Once connection is detected, submit to server
$scope.sync = function () {
var records = fetchAll();
//if connection exists
if (navigator && navigator.onLine && records.length) {
records.forEach(function (find, idx) {
//make service call for each record
$http({
url: '/api/finds',
method: 'POST',
data: find
}).then(function (res) {
//JS date sometime display differently, so toString keeps it more persistent
localStorage.removeItem(find.lcKey.toString());
records.splice(idx); //remove from records array
}, function (err) {
//error handling - service call failures
});
});
} else {
//error handling - alert user that a connection could not be established
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment