AppStorage: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 4: Line 4:
== Description ==
== Description ==


appStorage allows you to get data in ''key'', ''value'' pairs from the server. Data is maintained in the [https://dashboard.voltcloud.io/ Volt Dashboard]. This is useful for data you want to share with all users of your app: announcements, shared tables which change from time to time, dates or even code.
appStorage allows you to get data in ''key'', ''value'' pairs from the server. Data is maintained in the [https://dashboard.voltcloud.io/ VoltServer Dashboard]. This is useful for data you want to share with all users of your app: announcements, shared tables which change from time to time, dates or even code.


The ''value'' returned is a string. There is no size limit on ''value'', other than it be reasonable.
The ''value'' returned is a string. There is no size limit on ''value'', other than it be reasonable.


This feature is only available if your app is hosted on Volt.  
This feature is only available if your app is hosted on VoltServer.  


If your app does not have a live internet connection, the request will time out and an error message will be passed to ''callback''.
If your app does not have a live internet connection, the request will time out and an error message will be passed to ''callback''.
Line 14: Line 14:
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.
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.storage in the [https://docs.voltcloud.io/api/ Volt API].
appStorage is an alias for $volt.storage in the [https://docs.voltcloud.io/api/ VoltServer API].


== Properties and Methods ==
== Properties and Methods ==


{| class="wikitable"
{| class="wikitable"
|-
| clear(''callback'') || Clear all entries for the app. Dashboard only.
|-
|-
| getItem(''key'', ''callback'') || Get the item which is saved under ''key''. String.
| 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.
| getAllItems(''callback'') || Gets all the items for the app, as an array. Each element is a key,value pair.
|-
| removeItem(''key'', ''callback'') || Deletes item ''key''.  Dashboard only.
|-
| setItem(''key'', ''value'', ''callback'') || Sets item ''key'' to ''value''. Creates item if it does not exist.  Dashboard only.
|}
|}


== Example - BASIC ==
<tabber>
 
JavaScript=
<pre>
<syntaxhighlight lang="JavaScript">
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
</pre>
 
== Example - JavaScript ==
<pre>
butGetAllItemsApp.onclick = function() {
butGetAllItemsApp.onclick = function() {
     appStorage.getAllItems(getAllItemsCallback);
     appStorage.getAllItems(getAllItemsCallback);
Line 99: Line 71:
         NSB.MsgBox(data);
         NSB.MsgBox(data);
     }
     }
}
}</syntaxhighlight>
</pre>
|-|
BASIC=
<syntaxhighlight lang="vb.net">
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
</syntaxhighlight>
</tabber>


== Output ==
== Output ==

Latest revision as of 15:23, 25 February 2021

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

Description

appStorage allows you to get data in key, value pairs from the server. Data is maintained in the VoltServer Dashboard. This is useful for data you want to share with all users of your app: announcements, shared tables which change from time to time, dates or even code.

The value returned is a string. There is no size limit on value, other than it be reasonable.

This feature is only available if your app is hosted on VoltServer.

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.storage in the VoltServer API.

Properties and Methods

clear(callback) Clear all entries for the app. Dashboard only.
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.
removeItem(key, callback) Deletes item key. Dashboard only.
setItem(key, value, callback) Sets item key to value. Creates item if it does not exist. Dashboard only.

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

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

Output

(see appStorage sample)

Related Items

LocalStorage, SessionStorage, ServerStorage, SQL