added support for caching pages

This commit is contained in:
Chris Polis 2011-09-15 15:34:55 -07:00
parent 73766a769f
commit 39da010c0e
2 changed files with 35 additions and 17 deletions

View file

@ -11,7 +11,8 @@
$('#page_holder').pagify({
pages: ['about', 'usage', 'options'],
animation: 'fadeIn',
default: 'about'
default: 'about',
cache: true
});
});
</script>

View file

@ -19,26 +19,43 @@
};
this.settings = $.extend({}, this.defaults, options);
this.switchPage = function(page) {
page = page || window.location.hash.replace('#','');
// Run after loading if caching, otherwise run immediately
var runAfterLoading = function() {
self.switchPage = function(page) {
page = page || window.location.hash.replace('#','');
if(self.settings.cache) {
} else {
$.get(page+'.html', function(content) {
$(self).hide().html(content)[self.settings.animation]();
}, 'text');
if(self.settings.cache) {
$(self).hide().html(self.pages[page])[self.settings.animation]();
} else {
$.get(page+'.html', function(content) {
$(self).hide().html(content)[self.settings.animation]();
}, 'text');
}
}
}
// Respond to hash changes
$(window).bind('hashchange', function() {
self.switchPage();
});
// Respond to hash changes
$(window).bind('hashchange', function() {
self.switchPage();
});
// Load initial page - current hash or default page
if(window.location.hash) self.switchPage();
else if(self.settings.default) self.switchPage(self.settings.default);
// Load initial page - current hash or default page
if(window.location.hash) self.switchPage();
else if(this.settings.default) self.switchPage(this.settings.default);
};
// Cache pages
if(self.settings.cache) {
self.pages = {};
var pageLoads = self.settings.pages.length;
$.each(self.settings.pages, function(ndx, page) {
$.get(page+'.html', function(content) {
self.pages[page] = content;
pageLoads--;
if(!pageLoads) runAfterLoading();
}, 'text');
});
} else runAfterLoading();
};
})(jQuery);