OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 Copyright 2013 The Polymer Authors. All rights reserved. |
| 3 Use of this source code is governed by a BSD-style |
| 4 license that can be found in the LICENSE file. |
| 5 --> |
| 6 <!-- |
| 7 /** |
| 8 * @module Polymer Elements |
| 9 */ |
| 10 /** |
| 11 * Element access to localStorage. The "name" property |
| 12 * is the key to the data ("value" property) stored in localStorage. |
| 13 * |
| 14 * polymer-localstorage automatically saves the value to localStorage when |
| 15 * value is changed. Note that if value is an object auto-save will be |
| 16 * triggered only when value is a different instance. |
| 17 * |
| 18 * Example: |
| 19 * |
| 20 * <polymer-localstorage name="my-app-storage" value="{{value}}"></polymer-l
ocalstorage> |
| 21 * |
| 22 * @class polymer-localstorage |
| 23 * @blurb Element access to localStorage. |
| 24 * @snap http://polymer.github.io/polymer-localstorage/snap.png |
| 25 * @author The Polymer Authors |
| 26 * @categories Data |
| 27 * |
| 28 */ |
| 29 /** |
| 30 * Fired after it is loaded from localStorage. |
| 31 * |
| 32 * @event polymer-localstorage-load |
| 33 */ |
| 34 --> |
| 35 <link rel="import" href="../polymer/polymer.html"> |
| 36 |
| 37 <polymer-element name="polymer-localstorage" attributes="name value useRaw autoS
aveDisabled"> |
| 38 <template> |
| 39 <style> |
| 40 :host { |
| 41 display: none; |
| 42 } |
| 43 </style> |
| 44 </template> |
| 45 <script> |
| 46 Polymer('polymer-localstorage', { |
| 47 /** |
| 48 * The key to the data stored in localStorage. |
| 49 * |
| 50 * @attribute name |
| 51 * @type string |
| 52 * @default null |
| 53 */ |
| 54 name: '', |
| 55 /** |
| 56 * The data associated with the specified name. |
| 57 * |
| 58 * @attribute value |
| 59 * @type object |
| 60 * @default null |
| 61 */ |
| 62 value: null, |
| 63 /** |
| 64 * If true, the value is stored and retrieved without JSON processing. |
| 65 * |
| 66 * @attribute useRaw |
| 67 * @type boolean |
| 68 * @default false |
| 69 */ |
| 70 useRaw: false, |
| 71 /** |
| 72 * If true, auto save is disabled. |
| 73 * |
| 74 * @attribute autoSaveDisabled |
| 75 * @type boolean |
| 76 * @default false |
| 77 */ |
| 78 autoSaveDisabled: false, |
| 79 enteredView: function() { |
| 80 // wait for bindings are all setup |
| 81 this.async('load'); |
| 82 }, |
| 83 valueChanged: function() { |
| 84 if (this.loaded && !this.autoSaveDisabled) { |
| 85 this.save(); |
| 86 } |
| 87 }, |
| 88 load: function() { |
| 89 var v = localStorage.getItem(this.name); |
| 90 if (this.useRaw) { |
| 91 this.value = v; |
| 92 } else { |
| 93 // localStorage has a flaw that makes it difficult to determine |
| 94 // if a key actually exists or not (getItem returns null if the |
| 95 // key doesn't exist, which is not distinguishable from a stored |
| 96 // null value) |
| 97 // however, if not `useRaw`, an (unparsed) null value unambiguously |
| 98 // signals that there is no value in storage (a stored null value woul
d |
| 99 // be escaped, i.e. "null") |
| 100 // in this case we save any non-null current (default) value |
| 101 if (v === null) { |
| 102 if (this.value !== null) { |
| 103 this.save(); |
| 104 } |
| 105 } else { |
| 106 try { |
| 107 v = JSON.parse(v); |
| 108 } catch(x) { |
| 109 } |
| 110 this.value = v; |
| 111 } |
| 112 } |
| 113 this.loaded = true; |
| 114 this.asyncFire('polymer-localstorage-load'); |
| 115 }, |
| 116 /** |
| 117 * Saves the value to localStorage. |
| 118 * |
| 119 * @method save |
| 120 */ |
| 121 save: function() { |
| 122 var v = this.useRaw ? this.value : JSON.stringify(this.value); |
| 123 localStorage.setItem(this.name, v); |
| 124 } |
| 125 }); |
| 126 </script> |
| 127 </polymer-element> |
OLD | NEW |