User:Abel/experimental.js
Jump to navigation
Jump to search
Note: After saving, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
console.log("~");
var PersonalDataDataBaseName = "pdio";
var Pete = {id: 11345, name: {first: "Pete", last: "Rock"}, age: 52};
addRecord(PersonalDataDataBaseName, Pete);
addRecord.onsuccess(function{
recordUpdate(PersonalDataDataBaseName, Pete, 'phone', '+313555777');
});
function addRecord(pddbname, record){
var pddb = window.indexedDB.open(pddbname, 1);
pddb.addEventListener('error', (event) => {
console.log('Request error:', pddb.error);
}, false);
pddb.onupgradeneeded = function() {
var db = pddb.result;
var store = db.createObjectStore("MyObjectStore", {keyPath: "id"});
var index = store.createIndex("NameIndex", ["name.last", "name.first"]);
};
pddb.onsuccess = function() {
var db = pddb.result;
var tx = db.transaction("MyObjectStore", "readwrite");
var store = tx.objectStore("MyObjectStore");
var index = store.index("NameIndex");
store.put(record);
var recordcheck = index.get([record.name.first, record.name.last]);
recordcheck.onsuccess = function() {
console.log(recordcheck);
}
tx.oncomplete = function() {
db.close();
};
};
};
function recordUpdate(pddbname, record, field, value){
var pddb = window.indexedDB.open(pddbname, 1);
pddb.addEventListener('error', (event) => {
console.log('Request error:', pddb.error);
}, false);
pddb.onupgradeneeded = function() {
var db = pddb.result;
var store = db.createObjectStore("MyObjectStore", {keyPath: "id"});
var index = store.createIndex("NameIndex", ["name.last", "name.first"]);
};
pddb.onsuccess = function() {
// Start a new transaction
var db = pddb.result;
var tx = db.transaction("MyObjectStore", "readwrite");
var store = tx.objectStore("MyObjectStore");
var index = store.index("NameIndex");
var getRecord = index.get([record.name.first, record.name.last]);
getRecord.onsuccess = function() {
console.log("updating");
getRecord.result.field = value; // => "Bob"
store.put(getRecord.result);
getRecordAgain = index.get([record.name.first, record.name.last]);
getRecordAgain.onsuccess = function() {
console.log("hey there" + getRecordAgain); // => "Bob"
if ( getRecordAgain.result.field == value ) {
console.log("update succeeded");
} else {
console.log("update unsuccessful");
}
};
};
// Close the db when the transaction is done
tx.oncomplete = function() {
db.close();
};
};
};