Created
December 11, 2014 21:43
-
-
Save FirstWhack/9f7d128c40afa9dc3da6 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
function responseSuccess(data) { | |
localStorage.setItem('_echo_' + $.mobile.activePage.attr('id'), JSON.stringify(data)); | |
console.info('Stored '+ $.mobile.activePage.attr('id')+ ' response'); | |
} | |
// Check connection | |
function checkConnection() { | |
if (navigator && navigator.network && navigator.network.connection && navigator.network.connection.type) { | |
var networkState = navigator.network.connection.type; | |
if (networkState !== Connection.NONE) { | |
onOnline(); | |
} else { | |
onOffline(); | |
} | |
// When debug only (in desktop browser) | |
} else { | |
if (navigator.onLine) { | |
onOnline(); | |
} else { | |
onOffline(); | |
} | |
$('[dsid="footer"]').text('Browser mode'); | |
console.log('We are browser mode'); | |
} | |
} | |
// On offline | |
function onOffline() { | |
localStorage.setItem('_isOnline', 0); | |
console.log('We are offline'); | |
//$('[dsid="footer"]').text('Offline'); | |
} | |
// On online | |
function onOnline() { | |
localStorage.setItem('_isOnline', 1); | |
console.log('We are online'); | |
//$('[dsid="footer"]').text('Online'); | |
} | |
// List service execute | |
function executeService(service) { | |
checkConnection(); | |
var currentPage = $.mobile.activePage.attr('id'); | |
var isOnline = localStorage.getItem('_isOnline'); | |
var service = window[service]; | |
if (isOnline == 1) { | |
console.log('Working online, sending request'); | |
service.service.__requestOptions.echo = ''; | |
service.execute({}); | |
} else { | |
console.log('Working offline, fetching localStorage data'); | |
service.service.__requestOptions.echo = localStorage['_echo_'+currentPage] ? localStorage['_echo_'+currentPage] : ''; | |
service.execute(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment