Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Use chrome packaged app storage apis
Browse files Browse the repository at this point in the history
- Required when in packaged apps
  • Loading branch information
dfreedm committed Aug 6, 2013
1 parent 8ae5b55 commit 319c501
Showing 1 changed file with 59 additions and 40 deletions.
99 changes: 59 additions & 40 deletions polymer-localstorage/polymer-localstorage.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,47 +29,66 @@
</style>
</template>
<script>
Polymer('polymer-localstorage', {
/**
* The key to the data stored in localStorage.
*
* @attribute name
* @type string
* @default null
*/
/**
* The data associated with the specified name.
*
* @attribute value
* @type object
* @default null
*/
/**
* If true, the value is stored and retrieved without JSON processing.
*
* @attribute useRaw
* @type boolean
* @default false
*/
useRaw: false,
ready: function() {
this.load();
},
valueChanged: function() {
this.save();
},
load: function() {
var s = window.localStorage.getItem(this.name);
if (s && !this.useRaw) {
this.value = JSON.parse(s);
} else {
this.value = s;
(function() {
var CHROME_STORAGE = typeof window.chrome.storage !== 'undefined';
Polymer('polymer-localstorage', {
/**
* The key to the data stored in localStorage.
*
* @attribute name
* @type string
* @default null
*/
/**
* The data associated with the specified name.
*
* @attribute value
* @type object
* @default null
*/
/**
* If true, the value is stored and retrieved without JSON processing.
*
* @attribute useRaw
* @type boolean
* @default false
*/
useRaw: false,
ready: function() {
this.load();
if (CHROME_STORAGE) {
this._boundLoaded = this._loaded.bind(this);
}
},
valueChanged: function() {
this.save();
},
_loaded: function(s) {
if (s && !this.useRaw) {
this.value = JSON.parse(s);
} else {
this.value = s;
}
},
load: function() {
if (CHROME_STORAGE) {
window.chrome.storage.local.get(this.name, this._boundLoaded);
} else {
var s = window.localStorage.getItem(this.name);
this._loaded(s);
}
},
save: function() {
var val = this.useRaw ? this.value : JSON.stringify(this.value);
if (CHROME_STORAGE) {
var obj = {};
obj[name] = val;
window.chrome.storage.local.set(obj);
} else {
window.localStorage.setItem(this.name, val);
}
}
},
save: function() {
window.localStorage.setItem(this.name,
this.useRaw ? this.value : JSON.stringify(this.value));
}
});
});
</script>
</polymer-element>

0 comments on commit 319c501

Please sign in to comment.