ServerStorage: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
m (Ghenne moved page ServesStorage to ServerStorage: Typo)
(26 intermediate revisions by 2 users not shown)
Line 1: Line 1:
serverStorage(''string'' | ''variable'')
serverStorage.clear(''callback'')<br>
serverStorage.getItem(''key'', ''callback'')<br>
serverStorage.getAllItems(''key'', ''callback'')<br>
serverStorage.removeItem(''key'', ''callback'')<br>
serverStorage.setItem(''key'', ''value'', ''callback'')


== Description ==
== Description ==


serverStorage allows you to save string data on the server so it is available next time you run the program. An entry can be created by assigning to serverStorage.setItem(''variableName'', ''value''), where ''variableName'' is chosen by you. Data is retrieved the same way.  
ServerStorage 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 serverStorage.setItem(''key'', ''value''), where ''key'' is chosen by you. Data is retrieved a similar way.  The information in serverStorage is only available to the app itself.


''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.


serverStorage is shared among all apps deployed from the same web server. This can be useful if you have a family of apps which need to share data.
If your app does not have a live internet connection, the request will time out and an error message will be passed to ''callback''.


This feature is currently experimental. It only works on apps deployed to nsbapp.com. It will not work locally or on your server at this time. Since the data is on the nsbapp.com test server, do not assume that data you save will be permanently available.
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.


serverStorage is an alias for $volt.user.storage in the [https://docs.voltcloud.io/api/ Volt API].


== Properties and Methods ==


== Example ==
{| class="wikitable"
|-
| clear(''callback'') || Clear all entries for the app.
|-
| 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''.
|-
| setItem(''key'', ''value'', ''callback'') || Sets item ''key'' to ''value''. Creates item if it does not exist.
|}
 
== Example - BASIC ==


<pre>
<pre>
Rem serverStorage Save Example
Function butClear_onclick()
serverStorage.data = "This is Data."
  serverStorage.clear(done)
(run and exit program)
End Function
Rem serverStorage Retrieve Example
 
Print serverStorage.data
Function butGetAllItems_onclick()
  serverStorage.getAllItems(getAllItemsCallback)
Rem Test if Private Browsing is turned on
End Function
Try
 
   serverStorage.private = "test"
Function butGetItem_onclick()
Catch err
  serverStorage.getItem("SimpleString", getItemCallback)
  MsgBox "Please turn Private Browsing off"  
End Function
End Try
 
Function butRemoveItem_onclick()
  serverStorage.removeItem("SimpleString", done)
End Function
 
Function butSetItem_onclick()
  serverStorage.setItem("SimpleString", "ABCD", done)
  serverStorage.setItem("Number", 123, done)
  serverStorage.setItem("Array", [1, 2, 3, 4], done)
  serverStorage.setItem("Object", {firstName: "Eric", lastName: "Cartman"}, done)
End Function
 
Function done(error, data)
   If error Then
    If data = undefined Then data = {message: "Network Error"}
    MsgBox data.message
  Else
    console.log("success")
  End If
End Function


Rem Some additional samples:
Function getAllItemsCallback(error, data)
serverStorage.setItem("key", value) 'creates serverStorage.key with defined value
  If error Then
serverStorage["key"] = value 'alternative method for above
    If data = undefined Then data = {message: "Network Error"}
serverStorage.getItem("key") 'calls serverStorage.key...useful to check if it exists
    MsgBox data.message
serverStorage.removeItem("key") 'deletes serverStorage.key
  Else
delete serverStorage.key 'alternative method for above
    Print False
delete serverStorage["key"] 'another alternative method for above
    For Each key in data
serverStorage.clear() 'nuclear deletion of all serverStorage variables in the domain. Use with care.
      console.log(key, data[key])
serverStorage[DBRecords.rows.item(0)["ModsType"]] = "alpha"
      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>
butClear.onclick = function() {
    serverStorage.clear(done);
};
 
butGetAllItems.onclick = function() {
    serverStorage.getAllItems(getAllItemsCallback);
};
 
butGetItem.onclick = function() {
    serverStorage.getItem("SimpleString", getItemCallback);
};
 
butRemoveItem.onclick = function() {
    serverStorage.removeItem("SimpleString", done);
};
 
butSetItem.onclick = function() {
    serverStorage.setItem("SimpleString", "ABCD", done);
    serverStorage.setItem("Number", 123, done);
    serverStorage.setItem("Array", [1, 2, 3, 4], done);
    serverStorage.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) {
    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]);
        }
    }
}
 
function getItemCallback(error, data) {
    if (error) {
        if (data == undefined) {
            data = {
                message: "Network Error"
            };
        }
        NSB.MsgBox(data.message);
    } else {
        NSB.MsgBox(data);
    }
}
</pre>
</pre>


== Output ==
== Output ==


<pre>
(see serverStorage sample)
This is Data.
</pre>


== Related Items ==
== Related Items ==


[[localstorage|LocalStorage]], [[sessionstorage|SessionStorage]], [[sql|SQL]]
[[AppStorage|AppStorage]], [[localstorage|LocalStorage]], [[sessionstorage|SessionStorage]], [[sql|SQL]]


[[Category:Language Reference]]
[[Category:Language Reference]]


[[Category:Miscellaneous]]
[[Category:Miscellaneous]]

Revision as of 20:10, 23 January 2017

serverStorage.clear(callback)
serverStorage.getItem(key, callback)
serverStorage.getAllItems(key, callback)
serverStorage.removeItem(key, callback)
serverStorage.setItem(key, value, callback)

Description

ServerStorage 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 serverStorage.setItem(key, value), where key is chosen by you. Data is retrieved a similar way. The information in serverStorage is only available to the app itself.

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.

serverStorage is an alias for $volt.user.storage in the Volt API.

Properties and Methods

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

Example - BASIC

Function butClear_onclick()
  serverStorage.clear(done)
End Function

Function butGetAllItems_onclick()
  serverStorage.getAllItems(getAllItemsCallback)
End Function

Function butGetItem_onclick()
  serverStorage.getItem("SimpleString", getItemCallback)
End Function

Function butRemoveItem_onclick()
  serverStorage.removeItem("SimpleString", done)
End Function

Function butSetItem_onclick()
  serverStorage.setItem("SimpleString", "ABCD", done)
  serverStorage.setItem("Number", 123, done)
  serverStorage.setItem("Array", [1, 2, 3, 4], done)
  serverStorage.setItem("Object", {firstName: "Eric", lastName: "Cartman"}, done)
End Function

Function done(error, data)
  If error Then
    If data = undefined Then data = {message: "Network Error"}
    MsgBox data.message
  Else
    console.log("success")
  End If
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

butClear.onclick = function() {
    serverStorage.clear(done);
};

butGetAllItems.onclick = function() {
    serverStorage.getAllItems(getAllItemsCallback);
};

butGetItem.onclick = function() {
    serverStorage.getItem("SimpleString", getItemCallback);
};

butRemoveItem.onclick = function() {
    serverStorage.removeItem("SimpleString", done);
};

butSetItem.onclick = function() {
    serverStorage.setItem("SimpleString", "ABCD", done);
    serverStorage.setItem("Number", 123, done);
    serverStorage.setItem("Array", [1, 2, 3, 4], done);
    serverStorage.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) {
    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]);
        }
    }
}

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

Output

(see serverStorage sample)

Related Items

AppStorage, LocalStorage, SessionStorage, SQL