Wix.Data.Public

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:

  • Your app doesn’t need to store any sensitive information. Since the HTML document is public, make sure to only store information that’s displayed on the site.
  • Your app doesn’t need to store a lot of data (see the storage limitations below)

Wix.Data.Public methods are available in the Wix Editor and Viewer only – not in the Dashboard.

Storing data

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?

  • Strings – the layout/design used for the counter
  • json objects – animation settings or another group of related settings (you can store them as an object instead of storing each string separately)
  • Integers & numbers – starting number for the hit counter
  • Booleans – whether to count all visits to the site, or only new visitors

Setting the data’s scope

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.

Storage limits

You can store a certain amount of data in each scope:

  • Component scope (per compId): 400 characters in total, for all data in this scope – including spaces
  • App scope (per app instance ID): 1,000 characters in total, for all data in this scope – including spaces

set

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:

Copy
1
set(key, value, \[options\], onSuccess, \[onFailure\])

Parameters:

NameTypeDescription
key(required)StringKey to set. Each key must be unique within the given scope (see options, below).
value (required)Object/ String/ Boolean/ Integer/ NumberKey’s value.
optionsObjectOptions 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}
onFailureFunction(error)Errors thrown when: Your app exceeded the provided storage (1,000 characters for app scope, 400 for component scope), Invalid json

Example:

Copy
1
// Default setting for the startCounter field
2
Wix.Data.Public.set("startCounter", 1234, { scope: 'COMPONENT' },
3
function(d){ console.log(d) }, function(f) { console.log(f) }
4
);
5
6
// Once the user changes it to zero, set a new value for this key
7
Wix.Data.Public.set("startCounter", 0, { scope: 'COMPONENT' },
8
function(d) { console.log(d) }, function(f) { console.log(f) }
9
);

remove

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:

Copy
1
remove(key, \[options\], onSuccess, \[onFailure\])

Parameters:

NameTypeDescription
key (required)StringKey to remove
optionsObjectOptions 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}
onFailureFunction(error)Error is thrown when the key doesn’t exist

Example:

Copy
1
Wix.Data.Public.remove('text', {scope:'COMPONENT'}, function(d) {console.log(d)}, function(f){console.log(f)});

get

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:

Copy
1
get(key, \[options\], onSuccess, \[onFailure\])

Parameters:

NameTypeDescription
key (required)StringKey to get
optionsObjectOptions 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}
onFailureFunction(error)Error is thrown when the key doesn’t exist

Example:

Copy
1
Wix.Data.Public.get("startCounter", { scope: 'COMPONENT' }, function(d){console.log(d)}, function(f){console.log(f)});

Deprecated

Wix.Data.Public.getMulti – deprecated in SDK version 1.62.0

Was this helpful?
Yes
No