open-nomad/ui/app/utils/properties/local-storage.js
2018-08-15 15:18:38 -07:00

20 lines
680 B
JavaScript

import { computed } from '@ember/object';
// An Ember.Computed property that persists set values in localStorage
// and will attempt to get its initial value from localStorage before
// falling back to a default.
//
// ex. showTutorial: localStorageProperty('nomadTutorial', true),
export default function localStorageProperty(localStorageKey, defaultValue) {
return computed({
get() {
const persistedValue = window.localStorage.getItem(localStorageKey);
return persistedValue ? JSON.parse(persistedValue) : defaultValue;
},
set(key, value) {
window.localStorage.setItem(localStorageKey, JSON.stringify(value));
return value;
},
});
}