Skip to content

LocalStorage

Daniel Hindi edited this page Sep 14, 2016 · 11 revisions

#Shared Local Storage

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");
                });
            });
        });

buildfire.localStorage.setItem(key,value, callback)

This method will tell the app to save the value under this `key'. ###Parameters

  • 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

###Example

       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");
        });
Clone this wiki locally