LocalStorageProvider.html 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-state-LocalStorageProvider'>/**
  19. </span> * @class Ext.state.LocalStorageProvider
  20. * A Provider implementation which saves and retrieves state via the HTML5 localStorage object.
  21. * If the browser does not support local storage, an exception will be thrown upon instantiating
  22. * this class.
  23. */
  24. Ext.define('Ext.state.LocalStorageProvider', {
  25. /* Begin Definitions */
  26. extend: 'Ext.state.Provider',
  27. alias: 'state.localstorage',
  28. /* End Definitions */
  29. constructor: function(){
  30. var me = this;
  31. me.callParent(arguments);
  32. me.store = me.getStorageObject();
  33. me.state = me.readLocalStorage();
  34. },
  35. readLocalStorage: function(){
  36. var store = this.store,
  37. i = 0,
  38. len = store.length,
  39. prefix = this.prefix,
  40. prefixLen = prefix.length,
  41. data = {},
  42. key;
  43. for (; i &lt; len; ++i) {
  44. key = store.key(i);
  45. if (key.substring(0, prefixLen) == prefix) {
  46. data[key.substr(prefixLen)] = this.decodeValue(store.getItem(key));
  47. }
  48. }
  49. return data;
  50. },
  51. set : function(name, value){
  52. var me = this;
  53. me.clear(name);
  54. if (typeof value == &quot;undefined&quot; || value === null) {
  55. return;
  56. }
  57. me.store.setItem(me.prefix + name, me.encodeValue(value));
  58. me.callParent(arguments);
  59. },
  60. // private
  61. clear : function(name){
  62. this.store.removeItem(this.prefix + name);
  63. this.callParent(arguments);
  64. },
  65. getStorageObject: function(){
  66. try {
  67. var supports = 'localStorage' in window &amp;&amp; window['localStorage'] !== null;
  68. if (supports) {
  69. return window.localStorage;
  70. }
  71. } catch (e) {
  72. return false;
  73. }
  74. //&lt;debug&gt;
  75. Ext.Error.raise('LocalStorage is not supported by the current browser');
  76. //&lt;/debug&gt;
  77. }
  78. });
  79. </pre>
  80. </body>
  81. </html>