-
Notifications
You must be signed in to change notification settings - Fork 85
LocalStorage
Daniel Hindi edited this page May 9, 2018
·
11 revisions
While window.localStorage
is available to each plugin. It is secured to not be accessible to other plugins. However, there are scenarios where you would want to have a data shared and carried over from plugin to plugin. In this case one way to do this on the app level is buildfire.localStorage
. All data will be saved on the device as a string. This data will persist even if the device is rebooted.
Acting very similar to window.localStorage
. However, called asynchronously with a callback.
Example:
buildfire.localStorage.setItem("TEST",{a:"123"}, function(e,r){
console.log("item added");
buildfire.localStorage.getItem("TEST", function(e,r) {
console.log("item retrieved", JSON.stringify(r) );
buildfire.localStorage.removeItem("TEST", function(e,r) {
console.log("item removed");
});
});
});
This method will tell the app to save the value
under this key
.
- key (string): this is the unique identifier for your data. Make sure this is unique since other plugins are sharing the same storage there is potential overrides.
- value (string): this is the data you want to save. If a non-string is passed in a stringification will occur and when the item is retrieved again you it will be in string format. To return to its non-string type you will need to parse it. (see parseInt, parseFloat, JSON.parse ...etc)
- callback (function) : this is the function that will be called with the operation is completed.
Parameters
- error: if there is an error this will be set with the error object
- result: will return
true
if the save occurred successfully
buildfire.localStorage.setItem("myName","Daniel Hindi", function(error,result){
if(error)
console.error("something went wrong!", error);
else
console.log("All is well, data saved and other plugins can now access it");
});
This method will retrieve the previously saved value under this `key'.
- key (string): this is the unique identifier for your data.
- callback (function) : this is the function that will be called with the operation is completed.
Parameters
- error: if there is an error this will be set with the error object
- value: will return a string or undefined if not found.
buildfire.localStorage.getItem("myName", function(error,value){
if(error)
console.error("something went wrong!", error);
else if (value)
console.log("name: " , value);
else
console.log("nothing was previously saved");
});
This method will remove the previously saved value under this `key'.
- key (string): this is the unique identifier for your data.
- callback (function) : this is the function that will be called with the operation is completed.
Parameters
- error: if there is an error this will be set with the error object
- value: will return true if all went well.
buildfire.localStorage.removeItem("myName", function(error,result){
if(error)
console.error("something went wrong!", error);
else
console.log("value removed");
});