/* * 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; } };