Skip to content

Instantly share code, notes, and snippets.

@andremedeiros
Created November 17, 2010 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andremedeiros/703833 to your computer and use it in GitHub Desktop.
Save andremedeiros/703833 to your computer and use it in GitHub Desktop.
jquery.mobile.page.cache.js
(function ( $ ) {
var rscript = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;
$.mobile.page.prototype.options.cache = true;
$( "body" )
.delegate( ":mobile-page", "pagebeforecreate", function() {
var page = $( this ).data( "page" ),
cache = page.options.cache;
if ( cache === true ) {
return;
}
if ( cache === false ) {
page.cacheExpiration = -1;
} else if ( typeof cache === "number" ) {
page.cacheExpiration = $.now() + ( cache * 1000 );
}
})
.delegate( ":mobile-page", "pagebeforeshow", function( event, opts ) {
var $page = $( this ),
page = $page.data( "page" ),
expiration = page.cacheExpiration;
if ( !expiration || expiration === -1 ) {
return;
}
if ( $.now() > expiration ) {
var url = $page.data( 'url' ) || "/";
$.mobile.pageLoading();
$.ajax({
url: url,
async: false,
cache: false,
context: $page,
success: function( data ) {
this.page( "destroy" );
var html = $( "<div>" )
.append( data.replace( rscript, "" ) )
.find( "[data-role='page']" )
.html();
this
.html( html )
.data( 'url', url )
.page();
}
});
$.mobile.pageLoading( true );
}
})
.delegate( ":mobile-page", "pageshow", function() {
var page = $( this ).data( "page" );
if ( page.cacheExpiration === -1 ) {
page.cacheExpiration = $.now();
}
});
})( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment