X-Git-Url: http://git.cyclocoop.org/%7B%24admin_url%7Dmembres/cotisations/gestion/rappel_supprimer.php?a=blobdiff_plain;f=resources%2Fsrc%2Fmediawiki.storage.js;h=3405ba7747bb8a39127e544a327865a7529602af;hb=a1d6452fd28bc0c2b047ddb2e86e94326e5640fd;hp=e9b2c9d329b0e54e42be7c6978db7dea6ea9ae54;hpb=4077b57759756ecd0d25427ec9598feb28a28ac1;p=lhc%2Fweb%2Fwiklou.git diff --git a/resources/src/mediawiki.storage.js b/resources/src/mediawiki.storage.js index e9b2c9d329..3405ba7747 100644 --- a/resources/src/mediawiki.storage.js +++ b/resources/src/mediawiki.storage.js @@ -33,7 +33,7 @@ * * @param {string} key Key of item to retrieve * @return {string|null|boolean} String value, null if no value exists, or false - * if localStorage is not available. + * if storage is not available. */ SafeStorage.prototype.get = function ( key ) { try { @@ -47,7 +47,7 @@ * * @param {string} key Key name to store under * @param {string} value Value to be stored - * @return {boolean} Whether the save succeeded or not + * @return {boolean} The value was set */ SafeStorage.prototype.set = function ( key, value ) { try { @@ -61,7 +61,7 @@ * Remove a value from device storage. * * @param {string} key Key of item to remove - * @return {boolean} Whether the save succeeded or not + * @return {boolean} Whether the key was removed */ SafeStorage.prototype.remove = function ( key ) { try { @@ -71,6 +71,43 @@ return false; }; + /** + * Retrieve JSON object from device storage. + * + * @param {string} key Key of item to retrieve + * @return {Object|null|boolean} Object, null if no value exists or value + * is not JSON-parseable, or false if storage is not available. + */ + SafeStorage.prototype.getObject = function ( key ) { + var json = this.get( key ); + + if ( json === false ) { + return false; + } + + try { + return JSON.parse( json ); + } catch ( e ) {} + + return null; + }; + + /** + * Set an object value in device storage by JSON encoding + * + * @param {string} key Key name to store under + * @param {Object} value Object value to be stored + * @return {boolean} The value was set + */ + SafeStorage.prototype.setObject = function ( key, value ) { + var json; + try { + json = JSON.stringify( value ); + return this.set( key, json ); + } catch ( e ) {} + return false; + }; + /** * A wrapper for the HTML5 `localStorage` interface * that is safe to call on all browsers.