Created
May 2, 2012 13:44
-
-
Save iRyusa/2576587 to your computer and use it in GitHub Desktop.
jQuery jStorage Ajax Cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# / | |
def index | |
@last_news_id = News.last.id | |
# another stuff here | |
end | |
# /last_news | |
def last_news | |
@news = News.last(5).reverse | |
render 'last_news.html', :layout => false | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# index.html.erb | |
# include jQuery, jStorage, ... | |
<script type="text/javascript"> | |
var lastNewsId = <%= @last_news_id %>; | |
$(document).ready( | |
function() { | |
$.ajax({ | |
url: '/last_news', | |
cacheJStorage: true, | |
cacheKey: 'last_news_cache', | |
isCacheValid : function(){ | |
var cachedId = $.jStorage.get('last_news_id', 0); | |
return last_news_id == cachedId; | |
}, | |
success: function(reply) { | |
// You can now add your last news in the page | |
$.jStorage.set('last_news_id', last_news_id); | |
} | |
}); | |
} | |
); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment