added support for caching pages
This commit is contained in:
parent
73766a769f
commit
39da010c0e
|
@ -11,7 +11,8 @@
|
|||
$('#page_holder').pagify({
|
||||
pages: ['about', 'usage', 'options'],
|
||||
animation: 'fadeIn',
|
||||
default: 'about'
|
||||
default: 'about',
|
||||
cache: true
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
|
49
pagify.js
49
pagify.js
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue