added ajaxify popstate cache, so back/forward will just put back the already loaded page

v1.18.x
Julian Lam 10 years ago
parent 094cd6df32
commit 77d154bb8b

@ -1,9 +1,11 @@
"use strict"; "use strict";
var ajaxify = ajaxify || {}; var ajaxify = ajaxify || {
isPopState: false
};
$(document).ready(function() { $(document).ready(function() {
require(['templates'], function (templatesModule) { require(['templates', 'ajaxifyCache'], function (templatesModule, cache) {
/*global app, templates, utils, socket, translator, config, RELATIVE_PATH*/ /*global app, templates, utils, socket, translator, config, RELATIVE_PATH*/
var location = document.location || window.location, var location = document.location || window.location,
@ -13,7 +15,9 @@ $(document).ready(function() {
window.onpopstate = function (event) { window.onpopstate = function (event) {
if (event !== null && event.state && event.state.url !== undefined && !ajaxify.initialLoad) { if (event !== null && event.state && event.state.url !== undefined && !ajaxify.initialLoad) {
ajaxify.isPopState = true;
ajaxify.go(event.state.url, function() { ajaxify.go(event.state.url, function() {
ajaxify.isPopState = false;
$(window).trigger('action:popstate', {url: event.state.url}); $(window).trigger('action:popstate', {url: event.state.url});
}, true); }, true);
} }
@ -55,6 +59,12 @@ $(document).ready(function() {
// "quiet": If set to true, will not call pushState // "quiet": If set to true, will not call pushState
app.enterRoom(''); app.enterRoom('');
// If the url is in the cache, load from cache instead
if (cache.get(url)) { return true; }
else {
cache.url = ajaxify.currentPage;
}
$(window).off('scroll'); $(window).off('scroll');
if ($('#content').hasClass('ajaxifying') && apiXHR) { if ($('#content').hasClass('ajaxifying') && apiXHR) {
@ -114,6 +124,7 @@ $(document).ready(function() {
templates.parse(tpl_url, data, function(template) { templates.parse(tpl_url, data, function(template) {
translator.translate(template, function(translatedTemplate) { translator.translate(template, function(translatedTemplate) {
setTimeout(function() { setTimeout(function() {
cache.set();
$('#content').html(translatedTemplate); $('#content').html(translatedTemplate);
ajaxify.variables.parse(); ajaxify.variables.parse();

@ -0,0 +1,35 @@
'use strict';
/* globals define, app, ajaxify */
define('ajaxifyCache', function() {
var Cache = {
url: undefined,
DOM: undefined,
tempDOM: undefined
};
Cache.set = function() {
Cache.DOM = $('#content > *').detach();
};
Cache.get = function(url) {
if (url === Cache.url && ajaxify.isPopState) {
// Swap DOM elements
setTimeout(function() {
Cache.tempDOM = $('#content > *').detach();
$('#content').append(Cache.DOM);
Cache.DOM = Cache.tempDOM;
}, 100); // 100ms for realism! :sunglasses:
// Set the values that normally get set on ajaxify
Cache.url = ajaxify.currentPage;
ajaxify.currentPage = url;
return true;
} else {
return false;
}
};
return Cache;
});
Loading…
Cancel
Save