Using Wix.Data.Public methods, you can store public data in the site’s HTML document. This means that you may not need an external database for your app.
How do you know if our solution is sufficient? Use these methods if:
Wix.Data.Public methods are available in the Wix Editor and Viewer only – not in the Dashboard.
Each data entry is stored as a pair – a key and its value. Keys must be strings, and values can be strings, json objects, numbers, integers, and booleans.
Let’s use Wix Hit Counter as an example. This app is a small widget that counts the number of site visitors.
So what kind of data would you store for this app?
When you store data, you also define its scope as “app” or “component” – this determines where your data is available. By default, data is only available for the component that called the method, as well as that component’s settings. To make the data available globally for all app components, set the scope to app.
You can store a certain amount of data in each scope:
Stores a key with a value. If the key already exists, the previous value is overwritten with the new value. If the key doesn’t exist yet, it’s created now.
SDK Version: SDK 1.61.0+
Display: New Editor
Components: Widget, Pinned (aka Fixed-Position) Widget, Page, Settings Panel, Modal, Popup, Worker (app scope only)
For example: In the Wix Hit Counter app, the counter starts at 1234 by default. When a user adds the app, you would store “startCounter” as the key, and “1234” as the key’s value. If a user changes it to 0, you would use the set method again to replace the value with “0”.
Syntax:
Parameters:
Name | Type | Description |
---|---|---|
key(required) | String | Key to set. Each key must be unique within the given scope (see options, below). |
value (required) | Object/ String/ Boolean/ Integer/ Number | Key’s value. |
options | Object | Options for this method |
options.scope | “APP” or “COMPONENT” | Where the data is available: APP: All of the app’s components, COMPONENT (default): Only within this component and its settings |
onSuccess (required) | Function(result) | Returns a json object with the key and value you set { key1: value1} |
onFailure | Function(error) | Errors thrown when: Your app exceeded the provided storage (1,000 characters for app scope, 400 for component scope), Invalid json |
Example:
Deletes the key. Future attempts to access this key will raise an exception until something is stored again for this key using the set method.
SDK Version: SDK 1.61.0+
Components: Widget, Pinned (aka Fixed-Position) Widget, Page, Settings Panel, Modal, Popup, Worker (app scope only)
For example: Let’s say that Wix Hit Counter shows text after the number (“2,000 visitors”). Users can choose whether or not to show text (a boolean), and define the text to display. If the user chooses to just show the number, without any text after it, you can use remove to delete the “text” key.
Syntax:
Parameters:
Name | Type | Description |
---|---|---|
key (required) | String | Key to remove |
options | Object | Options for this method |
options.scope | “APP” or “COMPONENT” | The data's scope: APP: All of the app’s components, COMPONENT (default): Only within this component and its settings |
onSuccess (required) | Function(data) | Returns a json object of the removed data: { key1: value1} |
onFailure | Function(error) | Error is thrown when the key doesn’t exist |
Example:
Returns the value for a given key.
SDK Version: SDK 1.61.0+
Editor Version: New Editor
Components: Live Site, Widget, Pinned (aka Fixed-Position) Widget, Page, Settings Panel, Modal, Popup, Worker (app scope only)
For example, call this method when the iframe loads to display the app with the settings the user chose.
Syntax:
Parameters:
Name | Type | Description |
---|---|---|
key (required) | String | Key to get |
options | Object | Options for this method |
options.scope | “APP” or “COMPONENT” | Where the data is available: APP: All of the app’s components, COMPONENT (default): Only within this component and its settings |
onSuccess (required) | Function(data) | Returns a json object { key1:value1} |
onFailure | Function(error) | Error is thrown when the key doesn’t exist |
Example:
Wix.Data.Public.getMulti – deprecated in SDK version 1.62.0