Skip to content

Instantly share code, notes, and snippets.

@vandorjw
Created March 28, 2018 18:02
Show Gist options
  • Save vandorjw/53c6a360eb91a45825e4cd92ce5bec83 to your computer and use it in GitHub Desktop.
Save vandorjw/53c6a360eb91a45825e4cd92ce5bec83 to your computer and use it in GitHub Desktop.
class Storage {
/*
* This class exists to handle caching data between a page refresh.
*
* Usage example for setData:
*
* >> let myData = {"brand": "Example Brand", "brand_slug": "example_brand"}
*
* >> let store = new Storage('1', 'brands')
* >> store.setData(myData)
*
* Usage example for getData:
*
* >> let store = new Storage('1', 'brands')
* >> myData = store.getData()
*
*/
constructor(resultId, dataType)
this.resultId = resultId
this.dataType = dataType
this.storageKey = dataType + "_" + resultId
}
getData() {
if (sessionStorage.getItem(this.storageKey) === null) {
return null
} else {
data = JSON.parse(sessionStorage.getItem(this.storageKey))
}
}
setData(jsonObj) {
sessionStorage.setItem(this.storageKey, JSON.stringify(jsonObj))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment