Created
August 16, 2017 14:52
-
-
Save diogomachado/91f7aceff221f1e46f2009bc7e7b77a4 to your computer and use it in GitHub Desktop.
Livro - Exemplo indexedDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Cria um objeto | |
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.shimIndexedDB; | |
// Abre (ou cria) o banco | |
var open = indexedDB.open("Freeburguer", 1); | |
// Cria o esquema | |
open.onupgradeneeded = function() { | |
var db = open.result; | |
var store = db.createObjectStore("lanches", {keyPath: "id"}); | |
var index = store.createIndex("NomeIndex", ["nome.last", "nome.first"]); | |
}; | |
open.onsuccess = function() { | |
// Inicia a transação | |
var db = open.result; | |
var tx = db.transaction("lanches", "readwrite"); | |
var store = tx.objectStore("lanches"); | |
var index = store.index("NomeIndex"); | |
// Adiciona conteudo | |
store.put({id: 10001, nome: {first: "Big", last: "Bob"}, preco: 13.50}); | |
store.put({id: 10002, nome: {first: "Bigg", last: "Salada"}, preco: 22.90}); | |
// Consulta item por id | |
var pegarItemPorID = store.get(10001); | |
pegarItemPorID.onsuccess = function() { | |
console.log(getJohn.result.nome.first); | |
}; | |
// Consulta item por index | |
var pegarItemPorIndex = index.get(["Salada", "Bigg"]); | |
pegarItemPorIndex.onsuccess = function() { | |
console.log(getBob.result.nome.first); | |
}; | |
// Fecha o banco quando terminar | |
tx.oncomplete = function() { | |
db.close(); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Este é um exemplo de qual livro? Livro Javascript Guia Definitivo ?