Difference between revisions of "User:Abel/experimental.js"

From Wikibase Personal data
Jump to navigation Jump to search
m
m
Line 27: Line 27:
 
     var Pete = {id: 11345, name: {first: "Pete", last: "Rock"}, age: 52};
 
     var Pete = {id: 11345, name: {first: "Pete", last: "Rock"}, age: 52};
 
     store.put(Pete);
 
     store.put(Pete);
     recordUpdate(index, ["Pete", "Rock"], 'phoneNumber', '+674316');
+
     recordUpdate(store, index, ["Pete", "Rock"], 'phoneNumber', '+674316');
 
     // Query the data   
 
     // Query the data   
 
      
 
      
Line 36: Line 36:
 
}
 
}
  
function recordUpdate(store, record, field, value){
+
function recordUpdate(db, ref, record, field, value){
  
     var getRecord = store.get(record);
+
     var getRecord = ref.get(record);
  
 
     getRecord.onsuccess = function() {
 
     getRecord.onsuccess = function() {
 
       console.log("updating");
 
       console.log("updating");
 
       getRecord.result.field = value;  // => "Bob"
 
       getRecord.result.field = value;  // => "Bob"
       store.put(getRecord.result);
+
       db.put(getRecord.result);
       checkrecord(store, record, field)
+
       checkrecord(db, ref, record, field)
 
     };
 
     };
 
}
 
}
  
function checkRecord(store, record, field) {
+
function checkRecord(db, ref, record, field) {
       checkRecord = store.get(record);
+
       checkRecord = ref.get(record);
 
       checkRecord.onsuccess = function() {
 
       checkRecord.onsuccess = function() {
 
         console.log(getRecordAgain.result.phone);  // => "Bob"
 
         console.log(getRecordAgain.result.phone);  // => "Bob"
 
       };
 
       };
 
}
 
}

Revision as of 03:23, 25 April 2019

console.log("~");

var pddb = window.indexedDB.open("pdio", 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");

    // Add some data
    // var John = {id: 12345, name: {first: "John", last: "Doe"}, age: 42, phone: '+33123456789'};
    // var Bob = {id: 67890, name: {first: "Bob", last: "Smith"}, age: 35}
    // store.put(John);
    // store.put(Bob);
    var Pete = {id: 11345, name: {first: "Pete", last: "Rock"}, age: 52};
    store.put(Pete);
    recordUpdate(store, index, ["Pete", "Rock"], 'phoneNumber', '+674316');
    // Query the data   
    
    // Close the db when the transaction is done
    tx.oncomplete = function() {
        db.close();
    };
}

function recordUpdate(db, ref, record, field, value){

    var getRecord = ref.get(record);

    getRecord.onsuccess = function() {
       console.log("updating");
       getRecord.result.field = value;   // => "Bob"
       db.put(getRecord.result);
       checkrecord(db, ref, record, field)
    };
}

function checkRecord(db, ref, record, field) {
       checkRecord = ref.get(record);
       checkRecord.onsuccess = function() {
         console.log(getRecordAgain.result.phone);   // => "Bob"
       };
}