Advertisement
Ludwiq

indexedDB problem

Oct 26th, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function () {
  2.     "use strict";
  3.     WinJS.Utilities.startLog({ type: "error", tags: "winjs controls" });
  4.  
  5.  
  6.     WinJS.UI.Pages.define("/pages/home/home.html", {
  7.         // This function is called whenever a user navigates to this page. It
  8.         // populates the page elements with the app's data.
  9.         ready: function (element, options) {
  10.             // TODO: Initialize the page here.
  11.  
  12.             $("#data").append("<font color='red'>TEST</font>");
  13.  
  14.             $("#adddata").click(addSampleData);
  15.         }
  16.     });
  17.  
  18.     if ("indexedDB" in window) {
  19.         WinJS.log && WinJS.log("indexedDB available");
  20.     } else {
  21.         WinJS.log && WinJS.log("No indexedDb");
  22.     }
  23.  
  24.     var myData = new Object();
  25.  
  26.     myData.db = null;
  27.  
  28.     myData.open = function () {
  29.         var request = window.indexedDB.open("dataFromWeb", 1);
  30.  
  31.         request.onupgradeneeded = function (e) {
  32.             WinJS.log("Upgrading...");
  33.  
  34.             if (!myData.db) {
  35.                 myData.db = e.target.result;
  36.             }
  37.  
  38.             myData.db.createObjectStore("people");
  39.         }
  40.  
  41.         request.onsuccess = function (e) {
  42.             WinJS.log("Success!");
  43.             myData.db = e.target.result;
  44.         }
  45.  
  46.         request.onerror = function (e) {
  47.             WinJS.log("Error");
  48.         }
  49.     }
  50.  
  51.     myData.open();
  52.  
  53.     function addSampleData() {
  54.         //Assume db is a database variable opened earlier
  55.         var transaction = myData.db.transaction(["people"], "readwrite");
  56.         var store = transaction.objectStore("people");
  57.  
  58.         //Define a person
  59.         var person = {
  60.             name: "Piotrek",
  61.             email: "piotrek@piotrek.pl",
  62.             created: new Date()
  63.         }
  64.  
  65.         //Perform the add
  66.         var request = store.add(person, 1);
  67.         request.onerror = function (e) {
  68.             WinJS.log("Error", e.target.error.name);
  69.             //some type of error handler
  70.         }
  71.  
  72.         request.onsuccess = function (e) {
  73.             WinJS.log("Did it");
  74.         }
  75.     }
  76. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement