mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-08 06:55:46 +01:00
added ajaxify popstate cache, so back/forward will just put back the already loaded page
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
"use strict";
|
||||
|
||||
var ajaxify = ajaxify || {};
|
||||
var ajaxify = ajaxify || {
|
||||
isPopState: false
|
||||
};
|
||||
|
||||
$(document).ready(function() {
|
||||
require(['templates'], function (templatesModule) {
|
||||
require(['templates', 'ajaxifyCache'], function (templatesModule, cache) {
|
||||
/*global app, templates, utils, socket, translator, config, RELATIVE_PATH*/
|
||||
|
||||
var location = document.location || window.location,
|
||||
@@ -13,7 +15,9 @@ $(document).ready(function() {
|
||||
|
||||
window.onpopstate = function (event) {
|
||||
if (event !== null && event.state && event.state.url !== undefined && !ajaxify.initialLoad) {
|
||||
ajaxify.isPopState = true;
|
||||
ajaxify.go(event.state.url, function() {
|
||||
ajaxify.isPopState = false;
|
||||
$(window).trigger('action:popstate', {url: event.state.url});
|
||||
}, true);
|
||||
}
|
||||
@@ -55,6 +59,12 @@ $(document).ready(function() {
|
||||
// "quiet": If set to true, will not call pushState
|
||||
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');
|
||||
|
||||
if ($('#content').hasClass('ajaxifying') && apiXHR) {
|
||||
@@ -114,6 +124,7 @@ $(document).ready(function() {
|
||||
templates.parse(tpl_url, data, function(template) {
|
||||
translator.translate(template, function(translatedTemplate) {
|
||||
setTimeout(function() {
|
||||
cache.set();
|
||||
$('#content').html(translatedTemplate);
|
||||
|
||||
ajaxify.variables.parse();
|
||||
|
||||
35
public/src/modules/ajaxifyCache.js
Normal file
35
public/src/modules/ajaxifyCache.js
Normal file
@@ -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;
|
||||
});
|
||||
Reference in New Issue
Block a user