Created
September 4, 2014 12:41
-
-
Save hanssens/d887944c77674141ac63 to your computer and use it in GitHub Desktop.
Cordova/Phonegap - Reads a (json) file from the local cache and fetches it from a remote url if it doesn't exist yet
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
/* | |
* Licensed to the Apache Software Foundation (ASF) under one | |
* or more contributor license agreements. See the NOTICE file | |
* distributed with this work for additional information | |
* regarding copyright ownership. The ASF licenses this file | |
* to you under the Apache License, Version 2.0 (the | |
* "License"); you may not use this file except in compliance | |
* with the License. You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, | |
* software distributed under the License is distributed on an | |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
* KIND, either express or implied. See the License for the | |
* specific language governing permissions and limitations | |
* under the License. | |
*/ | |
var dataSource = null; | |
var app = { | |
initialize: function() { | |
this.bindEvents(); | |
}, | |
bindEvents: function() { | |
document.addEventListener('deviceready', this.onDeviceReady, false); | |
}, | |
onDeviceReady: function() { | |
app.receivedEvent('deviceready'); | |
// Synchronize the file | |
app.synchFile(); | |
navigator.splashscreen.hide(); | |
}, | |
synchFile: function () { | |
// first, check if the file exists locally | |
var fileName = "datasource"; | |
// tip: you can test the functionality below simply by uncommenting the line below | |
// localStorage.clear(); | |
// fetch the file from the local cache | |
dataSource = localStorage.getItem(fileName); | |
// check if something was found | |
if (dataSource === null) { | |
// nope - file doesn't exist, so fetch it remotely (synchronously) | |
var remoteFile = app.fetchRemoteFile("YOUR_REMOTE_URL"); | |
// persist it to the local cache | |
localStorage.setItem(fileName, remoteFile); | |
// and finally, read it in memory... | |
dataSource = remoteFile; | |
} | |
if (dataSource === null) { | |
console.log("Something went wrong... please add your error handling here..."); | |
return; | |
} | |
console.log("Finished, datasource globally available as:"); | |
console.log(dataSource); | |
}, | |
receivedEvent: function(id) { | |
var parentElement = document.getElementById(id); | |
var listeningElement = parentElement.querySelector('.listening'); | |
var receivedElement = parentElement.querySelector('.received'); | |
listeningElement.setAttribute('style', 'display:none;'); | |
receivedElement.setAttribute('style', 'display:block;'); | |
console.log('Received Event: ' + id); | |
}, | |
/* | |
* Custom Functions | |
*/ | |
fetchRemoteFile: function(path) { | |
var returnValue = null; | |
console.log("Fetching now!"); | |
$.ajax({ | |
url: path, | |
async: false, | |
dataType: "json", | |
type: "GET", | |
cache: false, | |
success: function (response) { | |
console.log("Function: fetchRemoteFile(" + path + "), success."); | |
returnValue = response; | |
}, | |
error: function (request, status, error) { | |
console.log('Function: fetchRemoteFile(), error: '); | |
console.log(request); | |
} | |
}); | |
return returnValue; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment