https://github.com/jareware/jsoncache
HTML5's localStorage caching layer for fetching JSON data with jQuery.
https://github.com/jareware/jsoncache
Last synced: about 1 year ago
JSON representation
HTML5's localStorage caching layer for fetching JSON data with jQuery.
- Host: GitHub
- URL: https://github.com/jareware/jsoncache
- Owner: jareware
- Created: 2011-07-26T10:33:28.000Z (almost 15 years ago)
- Default Branch: master
- Last Pushed: 2011-08-04T05:13:11.000Z (almost 15 years ago)
- Last Synced: 2025-06-07T23:56:25.539Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 296 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
Awesome Lists containing this project
README
=========
JSONCache
=========
JSONCache is a caching layer for fetching JSON data with jQuery. It
uses HTML5's window.localStorage to cache fetched JSON data to avoid
unnecessary network requests.
The primary goal of the library is to minimize network requests for
mobile devices.
Requirements
------------
- jQuery (versions ?)
- JSON.parse and JSON.stringify (available in the latest browsers or
e.g. with json2.js available at
https://github.com/douglascrockford/JSON-js)
- window.localStorage (available in the latest browsers)
Usage
-----
1. Make sure you have all the requirements.
2. Download the minified jsoncache-{version}.min.js file and include
it on your page:
3. Use the JSONCache.getCachedJSON function instead of jQuery.getJSON
to fetch your data:
JSONCache.getCachedJSON attempts to provide the same API as the
jQuery.ajax function.
Basic example:
::
JSONCache.getCachedJSON('http://example.com/data.json', {
success: function (data) {
// handle data
}
});
Example with error hooks to display status info to user:
::
// Message container to show info to user.
var message = $('#message');
JSONCache.getCachedJSON(
onerror: function (jqXHR, textStatus, errorThrown, tryNumber) {
message.text('Failed fetch number ' + tryNumber + '. Trying again...');
},
ongiveup: function (status) {
message.text('Network failure, cannot fetch data.');
},
success: function (data) {
message.text('Data fetched successfully!');
}
);
Testing
-------
JSONCache test suite uses Jasmine (
https://github.com/pivotal/jasmine/wiki ) for its test framework. The
tests can be run by typing:
::
make test # for dev and production
make test_dev # for dev
make test_prop # for production
The command line test runner uses PhantomJS (
http://www.phantomjs.org/ ). The tests can also be run by opening the
`tests/index.html` in a browser and clicking on the jasmine test
links.
Old QUnit tests are still available in the same index.html.
TODO
----
- Removing old entries based on cache size.
- Have to keep track of the cache size.
- Remove oldest entries based on timestamp.
- Catch browser quota errors (only supported on some browsers.)
- Add better support for user defined error handling.