Permalink
Browse files

add ajax cache

  • Loading branch information...
1 parent a77ed74 commit 8bf8b06d8b4567a83cd8dd9dd9d37739258cb3e4 @jserme committed Mar 11, 2015
Showing with 21 additions and 11 deletions.
  1. +21 −11 _layouts/default.html
View
@@ -87,22 +87,31 @@
window.history && window.history.pushState && window.history.replaceState &&
!navigator.userAgent.match(/((iPod|iPhone|iPad).+\bOS\s+[1-4]\D|WebApps\/.+CFNetwork)/)
- var request = function(url, callback) {
+ var cache = {}
+ var request = function(url, callback, isCache) {
var defaultJump = function() {
location.href = url
}
+ var success = function(data) {
+ try {
+ if (isCache) {
+ cache[url] = data
+ }
+ callback && callback(data)
+ } catch (e) {
+ defaultJump()
+ }
+ }
+
+ if (isCache) {
+ cache[url] && success(cache[url])
+ }
+
$.ajax({
url: url,
error: defaultJump,
- success: function(data) {
- try {
- callback && callback(data)
- } catch (e) {
- //出错直接跳转
- defaultJump()
- }
- }
+ success: success
})
}
@@ -141,7 +150,7 @@
var content = getContent(data, opts)
setContent(content, opts)
opts.callback && opts.callback()
- })
+ }, opts.cache)
})
return this.on('click', opts.selector || 'a', function(event) {
@@ -166,12 +175,13 @@
title: content.title
})
opts.callback && opts.callback()
- })
+ }, opts.cache)
})
}
})(jQuery)
$('body').ajaxLoadPage({
+ cache: true,
callback: function() {
window.DUOSHUO && window.DUOSHUO.init()
}

0 comments on commit 8bf8b06

Please sign in to comment.