2 Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
3 This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
4 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
5 The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
6 Code distributed by Google as part of the polymer project is also
7 subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
11 Element access to localStorage. The "name" property
12 is the key to the data ("value" property) stored in localStorage.
14 `core-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.
18 <core-localstorage name="my-app-storage" value="{{value}}"></core-localstorage>
20 @group Polymer Core Elements
21 @element core-localstorage
22 @blurb Element access to localStorage.
27 <link rel="import" href="../polymer/polymer.html">
29 <polymer-element name="core-localstorage" attributes="name value useRaw autoSaveDisabled" hidden>
32 Polymer('core-localstorage', {
35 * Fired when a value is loaded from localStorage.
36 * @event core-localstorage-load
40 * The key to the data stored in localStorage.
49 * The data associated with the specified name.
58 * If true, the value is stored and retrieved without JSON processing.
67 * If true, auto save is disabled.
69 * @attribute autoSaveDisabled
73 autoSaveDisabled: false,
75 attached: function() {
76 // wait for bindings are all setup
80 valueChanged: function() {
81 if (this.loaded && !this.autoSaveDisabled) {
87 var v = localStorage.getItem(this.name);
91 // localStorage has a flaw that makes it difficult to determine
92 // if a key actually exists or not (getItem returns null if the
93 // key doesn't exist, which is not distinguishable from a stored
95 // however, if not `useRaw`, an (unparsed) null value unambiguously
96 // signals that there is no value in storage (a stored null value would
97 // be escaped, i.e. "null")
98 // in this case we save any non-null current (default) value
100 if (this.value !== null) {
112 this.asyncFire('core-localstorage-load');
116 * Saves the value to localStorage.
121 var v = this.useRaw ? this.value : JSON.stringify(this.value);
122 localStorage.setItem(this.name, v);