Skip to content

Commit

Permalink
add ajax cache
Browse files Browse the repository at this point in the history
  • Loading branch information
jserme committed Mar 11, 2015
1 parent a77ed74 commit 8bf8b06
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions _layouts/default.html
Expand Up @@ -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
})
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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()
}
Expand Down

0 comments on commit 8bf8b06

Please sign in to comment.