You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+33-30Lines changed: 33 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,42 +14,41 @@ It could also be used completely different as well. To let the user input someth
14
14
This example shows how anki-persistence can be used to display the same random number on both sides of an Anki flash card. **All of these images depict the same note!** You can try it out yourself with [this exported Anki deck](examples/random-number/anki-persistence.apkg).
15
15
16
16
### Result
17
-
| Client | Front | Back |
18
-
| ----------------------:|:-----:|:----:|
19
-
|Web |||
20
-
|Android |||
17
+
| Client | Front | Back |
18
+
| -------:|:-----:|:----:|
19
+
| Web |||
20
+
| Android |||
21
21
| Android (card preview) |||
22
-
|iOS |||
22
+
| iOS |||
23
23
| iOS (card preview) |||
24
-
|Mac |||
24
+
| Mac |||
25
25
| Mac (card preview) |||
26
-
|Windows |||
27
-
| Windows (card preview) |||
28
-
|Linux |||
29
-
|Linux (card preview) |||
26
+
| Windows |||
27
+
| Windows (card type editor preview) |||
28
+
| Linux |||
29
+
| Linux (card preview) |||
30
30
31
-
**Note that Persistence is not available for Windows card preview (```Persistence.isAvailable()``` returns ```false```), thus a default (0.4) is chosen.**
31
+
**Note that Persistence is not available for Windows card type editor preview (```Persistence.isAvailable()``` returns ```false```), thus a default (0.4) is chosen.**
if (Persistence.isAvailable()) { // Check whether Persistence works on the client.
49
-
number =Persistence.load(); //Load a previously stored number. (In case {{FrontSide}} is used)
50
-
if (number ==null) { // If there was a previously stored number:
51
-
number =Math.random(); // 1. Create a random number.
52
-
Persistence.store(number);// 2. Store that number
46
+
var number =0.4; // Default to 0.4.
47
+
if (Persistence.isAvailable()) { // Check whether Persistence works on the client.
48
+
number =Persistence.getItem(); //Retrieve a previously stored number and override the default. (In case this is executed on the backside as well by {{FrontSide}})
49
+
if (number ==null) { // If there was no number stored previously:
50
+
number =Math.random(); // 1. Create a random number and override the default.
51
+
Persistence.setItem(number); // 2. Store that number
53
52
}
54
53
}
55
54
window.front.appendChild(document.createTextNode(number)); // Print the number.
@@ -60,20 +59,19 @@ window.front.appendChild(document.createTextNode(number)); // Print the number.
if (Persistence.isAvailable()) { // Check whether Persistence works on the client.
75
-
number =Persistence.load(); //Load the previously stored number
76
-
Persistence.store(null);// Clear the storage, so a new random number will be created on the next card.
71
+
var number =0.4; // Default to 0.4.
72
+
if (Persistence.isAvailable()) { // Check whether Persistence works on the client.
73
+
number =Persistence.getItem(); //Retrieve the previously stored number and override the default.
74
+
Persistence.clear(); // Clear the storage, so a new random number will be created on the next card.
77
75
}
78
76
window.back.appendChild(document.createTextNode(number)); // Print the number.
79
77
</script>
@@ -107,14 +105,19 @@ Other methods:
107
105
108
106
| Name | Description |
109
107
| -----------------------------:|:----------- |
110
-
|```Persistence.store(data)```| Persists the data, so it can be retrieved later. |
111
-
|```Persistence.load()```| Retrieves previously stored data. If no data has been stored yet, null is returned. |
108
+
|```Persistence.clear()```| Removes all previously persisted key-value pairs. |
109
+
|```Persistence.getItem(key)```| Retrieves the data associated with the key. If no data is associated to the given key, null is returned. |
110
+
|```Persistence.getItem())```| Retrieves the data associated with a default key. |
111
+
|```Persistence.setItem(key, data)```| Persists the key-value pair. |
112
+
|```Persistence.setItem(data)```| Persists the value using a default key. |
113
+
|```Persistence.removeItem(key)```| Removes the data associated with the key. If no data is associated to the given key, nothing happens. |
114
+
|```Persistence.removeItem()```| Removes the data associated with a default key. |
112
115
113
116
*Some implementations of Persistence may use JSON.stringify and JSON.parse in the process of persisting and retrieving data.*
114
117
115
118
### Clear storage
116
119
117
-
```Persistence.store``` may persist data across cards, this should be stopped by calling ```Persistence.store(null)``` at the end of the backside. (If this gets called on the frontside's beginning instead, you cannot use anki's ```{{FrontSide}}``` special field in the backside *- because this would delete the persisted data*)
120
+
```Persistence.setItem``` may persist data across cards, this should be stopped by calling ```Persistence.clear()``` at the end of the backside. (If this gets called on the frontside's beginning instead, you cannot use anki's ```{{FrontSide}}``` special field in the backside *- because this would delete the persisted data*)
0 commit comments