AppStorage: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
Line 60: Line 60:
== Example - JavaScript ==
== Example - JavaScript ==
<pre>
<pre>
butClear.onclick = function() {
butGetAllItemsApp.onclick = function() {
    appStorage.clear(done);
    appStorage.getAllItems(getAllItemsCallback);
};
};


butGetAllItems.onclick = function() {
butGetItemApp.onclick = function() {
    appStorage.getAllItems(getAllItemsCallback);
    appStorage.getItem("SimpleString", getItemCallback);
};
};
 
butGetItem.onclick = function() {
    appStorage.getItem("SimpleString", getItemCallback);
};
 
butRemoveItem.onclick = function() {
    appStorage.removeItem("SimpleString", done);
};
 
butSetItem.onclick = function() {
    appStorage.setItem("SimpleString", "ABCD", done);
    appStorage.setItem("Number", 123, done);
    appStorage.setItem("Array", [1, 2, 3, 4], done);
    appStorage.setItem("Object", {
        firstName: "Eric",
        lastName: "Cartman"
    }, done);
};
 
function done(error, data) {
    if (error) {
        if (data == undefined) {
            data = {
                message: "Network Error"
            };
        }
        NSB.MsgBox(data.message);
    } else {
        console.log("success");
    }
}


function getAllItemsCallback(error, data) {
function getAllItemsCallback(error, data) {

Revision as of 19:28, 23 January 2017

appStorage.getItem(key, callback)
appStorage.getAllItems(key, callback)

Description

appStorage allows you to save data in key, value pairs on the server so it is available next time you run the program. An entry can be created by assigning to appStorage.setItem(variableName, value), where variableName is chosen by you. Data is retrieved a similar way.

value can be a string, number, array or object. There is no size limit on value, other than it be reasonable.

This feature is only available if your app is hosted on Volt. If your app does not have a live internet connection, the request will time out and an error message will be passed to callback.

Since the calls access information on a server, they are asynchronous (so your app does not look up while the call is being processed). When the call is complete, the function named in callback is called in your app. It passes two parameters: (error, data). If the call is successful, error is empty and your results are in data. If the call is unsuccessful, error is not empty and the error message is in data.message.

appStorage is an alias for $volt.user.storage.

Properties and Methods

getItem(key, callback) Get the item which is saved under key. String.
getAllItems(callback) Gets all the items for the app, as an array. Each element is a key,value pair.

Example - BASIC

Function butGetAllItemsApp_onclick()
  appStorage.getAllItems(getAllItemsCallback)
End Function

Function butGetItemApp_onclick()
  appStorage.getItem("SimpleString", getItemCallback)
End Function

Function getAllItemsCallback(error, data) 
  If error Then
    If data = undefined Then data = {message: "Network Error"}
    MsgBox data.message
  Else
    Print False
    For Each key in data
      console.log(key, data[key])
      k = [key, key][0]
      Print k & ": " & data[key].toString()
    Next
    End If
End Function

Function getItemCallback(error, data) 
  If error Then
    If data = undefined Then data = {message: "Network Error"}
    MsgBox data.message
  Else
    MsgBox data
  End If
End Function

Example - JavaScript

butGetAllItemsApp.onclick = function() {
     appStorage.getAllItems(getAllItemsCallback);
 };

 butGetItemApp.onclick = function() {
     appStorage.getItem("SimpleString", getItemCallback);
 };

function getAllItemsCallback(error, data) {
    if (error) {
        if (data == undefined) {
            data = {
                message: "Network Error"
            };
        }
        NSB.MsgBox(data.message);
    } else {
        NSB.Print((false) + "<br>");
        for (key in data) {
            console.log(key, data[key]);
            k = [key, key][0];
            NSB.Print(((k) + (": ") + (data[key].toString())) + "<br>");
        }
    }
}

function getItemCallback(error, data) {
    if (error) {
        if (data == undefined) {
            data = {
                message: "Network Error"
            };
        }
        NSB.MsgBox(data.message);
    } else {
        NSB.MsgBox(data);
    }
}

Output

(see appStorage sample)

Related Items

LocalStorage, SessionStorage, ServerStorage, SQL