Skip to content

Commit c49d443

Browse files
committed
chore: minor edits
1 parent 0426f79 commit c49d443

File tree

2 files changed

+63
-64
lines changed

2 files changed

+63
-64
lines changed

indexeddb-examples/idbcursor/scripts/main.js

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// create an instance of a db object for us to store the IDB data in
2-
var db;
2+
let db;
33

4-
var records = [
4+
const records = [
55
{ albumTitle: "Power windows", year: 1985 },
66
{ albumTitle: "Grace under pressure", year: 1984 },
77
{ albumTitle: "Signals", year: 1982 },
@@ -17,12 +17,12 @@ var records = [
1717

1818
// all the variables we need for the app
1919

20-
var list = document.querySelector("ul");
21-
var advance = document.querySelector(".advance");
22-
var useContinue = document.querySelector(".continue");
23-
var useDelete = document.querySelector(".delete");
24-
var update = document.querySelector(".update");
25-
var changeDirection = document.querySelector(".direction");
20+
const list = document.querySelector("ul");
21+
const advance = document.querySelector(".advance");
22+
const useContinue = document.querySelector(".continue");
23+
const useDelete = document.querySelector(".delete");
24+
const update = document.querySelector(".update");
25+
const changeDirection = document.querySelector(".direction");
2626

2727
window.onload = function () {
2828
// In the following line, you should include the prefixes of implementations you want to test.
@@ -31,7 +31,7 @@ window.onload = function () {
3131
window.mozIndexedDB ||
3232
window.webkitIndexedDB ||
3333
window.msIndexedDB;
34-
// DON'T use "var indexedDB = ..." if you're not in a function.
34+
// DON'T use "const indexedDB = ..." if you're not in a function.
3535
// Moreover, you may need references to some window.IDB* objects:
3636
window.IDBTransaction =
3737
window.IDBTransaction ||
@@ -41,31 +41,31 @@ window.onload = function () {
4141
window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange;
4242
// (Mozilla has never prefixed these objects, so we don't need window.mozIDB*)
4343

44-
var DBOpenRequest = window.indexedDB.open("albumLists", 1);
44+
const DBOpenRequest = window.indexedDB.open("albumLists", 1);
4545

4646
DBOpenRequest.onsuccess = function (event) {
4747
db = DBOpenRequest.result;
4848
populateData();
4949
};
5050

5151
DBOpenRequest.onupgradeneeded = function (event) {
52-
var db = event.target.result;
52+
const db = event.target.result;
5353

5454
db.onerror = function (event) {
5555
note.innerHTML += "<li>Error loading database.</li>";
5656
};
5757

58-
var objectStore = db.createObjectStore("rushAlbumList", {
58+
const objectStore = db.createObjectStore("rushAlbumList", {
5959
keyPath: "albumTitle",
6060
});
6161
objectStore.createIndex("year", "year", { unique: false });
6262
};
6363

6464
function populateData() {
65-
var transaction = db.transaction(["rushAlbumList"], "readwrite");
66-
var objectStore = transaction.objectStore("rushAlbumList");
65+
const transaction = db.transaction(["rushAlbumList"], "readwrite");
66+
const objectStore = transaction.objectStore("rushAlbumList");
6767
for (i = 0; i < records.length; i++) {
68-
var request = objectStore.put(records[i]);
68+
const request = objectStore.put(records[i]);
6969
}
7070

7171
transaction.oncomplete = function () {
@@ -79,13 +79,13 @@ window.onload = function () {
7979

8080
function displayData() {
8181
list.innerHTML = "";
82-
var transaction = db.transaction(["rushAlbumList"], "readonly");
83-
var objectStore = transaction.objectStore("rushAlbumList");
82+
const transaction = db.transaction(["rushAlbumList"], "readonly");
83+
const objectStore = transaction.objectStore("rushAlbumList");
8484

8585
objectStore.openCursor().onsuccess = function (event) {
86-
var cursor = event.target.result;
86+
const cursor = event.target.result;
8787
if (cursor) {
88-
var listItem = document.createElement("li");
88+
const listItem = document.createElement("li");
8989
listItem.innerHTML =
9090
"<strong>" +
9191
cursor.value.albumTitle +
@@ -110,13 +110,13 @@ window.onload = function () {
110110

111111
function advanceResult() {
112112
list.innerHTML = "";
113-
var transaction = db.transaction(["rushAlbumList"], "readonly");
114-
var objectStore = transaction.objectStore("rushAlbumList");
113+
const transaction = db.transaction(["rushAlbumList"], "readonly");
114+
const objectStore = transaction.objectStore("rushAlbumList");
115115

116116
objectStore.openCursor().onsuccess = function (event) {
117-
var cursor = event.target.result;
117+
const cursor = event.target.result;
118118
if (cursor) {
119-
var listItem = document.createElement("li");
119+
const listItem = document.createElement("li");
120120
listItem.innerHTML =
121121
"<strong>" +
122122
cursor.value.albumTitle +
@@ -136,21 +136,21 @@ window.onload = function () {
136136

137137
function deleteResult() {
138138
list.innerHTML = "";
139-
var transaction = db.transaction(["rushAlbumList"], "readwrite");
140-
var objectStore = transaction.objectStore("rushAlbumList");
139+
const transaction = db.transaction(["rushAlbumList"], "readwrite");
140+
const objectStore = transaction.objectStore("rushAlbumList");
141141

142142
objectStore.openCursor().onsuccess = function (event) {
143-
var cursor = event.target.result;
143+
const cursor = event.target.result;
144144
if (cursor) {
145145
if (cursor.value.albumTitle === "Grace under pressure") {
146-
var request = cursor.delete();
146+
const request = cursor.delete();
147147
request.onsuccess = function () {
148148
console.log(
149149
"Deleted that mediocre album from 1984. Even Power windows is better."
150150
);
151151
};
152152
} else {
153-
var listItem = document.createElement("li");
153+
const listItem = document.createElement("li");
154154
listItem.innerHTML =
155155
"<strong>" +
156156
cursor.value.albumTitle +
@@ -171,23 +171,23 @@ window.onload = function () {
171171

172172
function updateResult() {
173173
list.innerHTML = "";
174-
var transaction = db.transaction(["rushAlbumList"], "readwrite");
175-
var objectStore = transaction.objectStore("rushAlbumList");
174+
const transaction = db.transaction(["rushAlbumList"], "readwrite");
175+
const objectStore = transaction.objectStore("rushAlbumList");
176176

177177
objectStore.openCursor().onsuccess = function (event) {
178-
var cursor = event.target.result;
178+
const cursor = event.target.result;
179179
if (cursor) {
180180
if (cursor.value.albumTitle === "A farewell to kings") {
181-
var updateData = cursor.value;
181+
const updateData = cursor.value;
182182

183183
updateData.year = 2050;
184-
var request = cursor.update(updateData);
184+
const request = cursor.update(updateData);
185185
request.onsuccess = function () {
186186
console.log("A better album year?");
187187
};
188188
}
189189

190-
var listItem = document.createElement("li");
190+
const listItem = document.createElement("li");
191191
listItem.innerHTML =
192192
"<strong>" +
193193
cursor.value.albumTitle +
@@ -208,13 +208,13 @@ window.onload = function () {
208208

209209
function backwards() {
210210
list.innerHTML = "";
211-
var transaction = db.transaction(["rushAlbumList"], "readonly");
212-
var objectStore = transaction.objectStore("rushAlbumList");
211+
const transaction = db.transaction(["rushAlbumList"], "readonly");
212+
const objectStore = transaction.objectStore("rushAlbumList");
213213

214214
objectStore.openCursor(null, "prev").onsuccess = function (event) {
215-
var cursor = event.target.result;
215+
const cursor = event.target.result;
216216
if (cursor) {
217-
var listItem = document.createElement("li");
217+
const listItem = document.createElement("li");
218218
listItem.innerHTML =
219219
"<strong>" +
220220
cursor.value.albumTitle +

indexeddb-examples/idbindex/scripts/main.js

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
// create an instance of a db object for us to store the IDB data in
2-
var db;
2+
let db;
3+
let activeIndex;
34

4-
var activeIndex;
5-
6-
var contacts = [
5+
const contacts = [
76
{
87
id: 1,
98
fName: "Brian",
@@ -108,7 +107,7 @@ var contacts = [
108107

109108
// all the variables we need for the app
110109

111-
var tableEntry = document.querySelector("tbody");
110+
const tableEntry = document.querySelector("tbody");
112111

113112
window.onload = function () {
114113
// In the following line, you should include the prefixes of implementations you want to test.
@@ -117,7 +116,7 @@ window.onload = function () {
117116
window.mozIndexedDB ||
118117
window.webkitIndexedDB ||
119118
window.msIndexedDB;
120-
// DON'T use "var indexedDB = ..." if you're not in a function.
119+
// DON'T use "const indexedDB = ..." if you're not in a function.
121120
// Moreover, you may need references to some window.IDB* objects:
122121
window.IDBTransaction =
123122
window.IDBTransaction ||
@@ -127,21 +126,21 @@ window.onload = function () {
127126
window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange;
128127
// (Mozilla has never prefixed these objects, so we don't need window.mozIDB*)
129128

130-
var DBOpenRequest = window.indexedDB.open("contactsList", 1);
129+
const DBOpenRequest = window.indexedDB.open("contactsList", 1);
131130

132131
DBOpenRequest.onsuccess = function (event) {
133132
db = DBOpenRequest.result;
134133
populateData();
135134
};
136135

137136
DBOpenRequest.onupgradeneeded = function (event) {
138-
var db = event.target.result;
137+
const db = event.target.result;
139138

140139
db.onerror = function (event) {
141140
console.log("Error loading database.");
142141
};
143142

144-
var objectStore = db.createObjectStore("contactsList", { keyPath: "id" });
143+
const objectStore = db.createObjectStore("contactsList", { keyPath: "id" });
145144
objectStore.createIndex("lName", "lName", { unique: false });
146145
objectStore.createIndex("fName", "fName", { unique: false });
147146
objectStore.createIndex("jTitle", "jTitle", { unique: false });
@@ -152,20 +151,20 @@ window.onload = function () {
152151
};
153152

154153
function populateData() {
155-
var transaction = db.transaction(["contactsList"], "readwrite");
156-
var objectStore = transaction.objectStore("contactsList");
154+
const transaction = db.transaction(["contactsList"], "readwrite");
155+
const objectStore = transaction.objectStore("contactsList");
157156
for (i = 0; i < contacts.length; i++) {
158-
var request = objectStore.put(contacts[i]);
157+
objectStore.put(contacts[i]);
159158
}
160159

161160
transaction.oncomplete = function () {
162161
displayDataByKey();
163162
};
164163
}
165164

166-
var thControls = document.querySelectorAll("th");
165+
const thControls = document.querySelectorAll("th");
167166
for (i = 0; i < thControls.length; i++) {
168-
var activeThead = thControls[i];
167+
const activeThead = thControls[i];
169168
activeThead.onclick = function (e) {
170169
activeIndex = e.target.innerHTML;
171170
if (activeIndex == "ID") {
@@ -192,13 +191,13 @@ window.onload = function () {
192191

193192
function displayDataByKey() {
194193
tableEntry.innerHTML = "";
195-
var transaction = db.transaction(["contactsList"], "readonly");
196-
var objectStore = transaction.objectStore("contactsList");
194+
const transaction = db.transaction(["contactsList"], "readonly");
195+
const objectStore = transaction.objectStore("contactsList");
197196

198197
objectStore.openCursor().onsuccess = function (event) {
199-
var cursor = event.target.result;
198+
const cursor = event.target.result;
200199
if (cursor) {
201-
var tableRow = document.createElement("tr");
200+
const tableRow = document.createElement("tr");
202201
tableRow.innerHTML =
203202
"<td>" +
204203
cursor.value.id +
@@ -235,40 +234,40 @@ window.onload = function () {
235234

236235
function displayDataByIndex(activeIndex) {
237236
tableEntry.innerHTML = "";
238-
var transaction = db.transaction(["contactsList"], "readonly");
239-
var objectStore = transaction.objectStore("contactsList");
237+
const transaction = db.transaction(["contactsList"], "readonly");
238+
const objectStore = transaction.objectStore("contactsList");
240239

241-
var myIndex = objectStore.index(activeIndex);
240+
const myIndex = objectStore.index(activeIndex);
242241

243242
console.log(myIndex.name);
244243
console.log(myIndex.objectStore);
245244
console.log(myIndex.keyPath);
246245
console.log(myIndex.multiEntry);
247246
console.log(myIndex.unique);
248247

249-
var countRequest = myIndex.count();
248+
const countRequest = myIndex.count();
250249
countRequest.onsuccess = function () {
251250
console.log(countRequest.result);
252251
};
253252

254253
if (activeIndex == "fName") {
255-
var getRequest = myIndex.get("Mr");
254+
const getRequest = myIndex.get("Mr");
256255
getRequest.onsuccess = function () {
257256
console.log(getRequest.result);
258257
};
259258
}
260259

261260
if (activeIndex == "lName") {
262-
var getKeyRequest = myIndex.getKey("Bungle");
261+
const getKeyRequest = myIndex.getKey("Bungle");
263262
getKeyRequest.onsuccess = function () {
264263
console.log(getKeyRequest.result);
265264
};
266265
}
267266

268267
myIndex.openCursor().onsuccess = function (event) {
269-
var cursor = event.target.result;
268+
const cursor = event.target.result;
270269
if (cursor) {
271-
var tableRow = document.createElement("tr");
270+
const tableRow = document.createElement("tr");
272271
tableRow.innerHTML =
273272
"<td>" +
274273
cursor.value.id +

0 commit comments

Comments
 (0)