Skip to content

Instantly share code, notes, and snippets.

@tshm
Created March 20, 2012 12:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tshm/2134907 to your computer and use it in GitHub Desktop.
Save tshm/2134907 to your computer and use it in GitHub Desktop.
agilityjs - localStorage adapter
/*
[MIT licensed](http://en.wikipedia.org/wiki/MIT_License)
*/
// custom agilityjs adapter for localstorage
(function($$, console) {
'use strict';
$$.adapter.localStorage = function(_params) {
var storageKey = (this._data.persist.baseUrl || '') + this._data.persist.collection,
storageStr = localStorage[storageKey],
items = (storageStr ? JSON.parse(storageStr) : {});
//
if ('GET' === _params.type) {
if (undefined !== _params.id) { // normal get
if ('object' === typeof items[_params.id]) {
_params.success(items[_params.id]);
} else {
_params.error();
}
} else { // gather call
_params.success(items);
}
} else if ('DELETE' === _params.type) {
delete items[_params.id];
localStorage[storageKey] = JSON.stringify(items);
} else if ('PUT' === _params.type || 'POST' === _params.type) {
if (undefined === _params.id) {
_params.id = (new Date()).getTime();
_params.data.id = _params.id;
}
items[_params.id] = _params.data;
//_params.success({id:_params.id});
localStorage[storageKey] = JSON.stringify(items);
} else {
_params.error();
}
_params.complete();
};
})(window.agility, window.console);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment