|
|
这里有indexeddb的一些使用,各种操作基本都包括,有的被注释
var key = null;
// Simply open the database once so that it is created with the required tables
$.indexedDB("MyECommerceSite", {
"schema": {
"1": function(versionTransaction){
var catalog = versionTransaction.createObjectStore("catalog", {
"keyPath": "itemId"
});
catalog.createIndex("price");
},
// This was added in the next version of the site
"2": function(versionTransaction){
var cart = versionTransaction.createObjectStore("cart", {
"autoIncrement": true
});
cart.createIndex("itemId");
var wishlist = versionTransaction.createObjectStore("wishlist", {
"autoIncrement": false,
"keyPath": "itemId"
});
wishlist.createIndex("name");
}
}
}).done(function(){
// Once the DB is opened with the object stores set up, show data from all tables
window.setTimeout(function(){
loadFromDB("cart");
loadFromDB("wishlist");
downloadCatalog();
}, 200);
});
$.indexedDB("www.huffmans.cn", {
"schema": {
"1": function(versionTransaction){
var oflb = versionTransaction.createObjectStore("oflb", {
//"keyPath": "name",
//"autoIncrement": true //do pk
});
oflb.createIndex("name"); //support mutiples index
}
}
}).done(function(){
// Once the DB is opened with the object stores set up, show data from all tables
window.setTimeout(function(){
//loadFromDB("cart");
//loadFromDB("wishlist");
//downloadCatalog();
downloadxml();
//setTimeout(loadFromOflb('oflb'),200);
//updoflb2('oflb');
//loadFromOflb('oflb');
usedbidx();
}, 200);
});
function downloadxml(){
var setting = {
url:'<%=basePath%>github/indexdb/tdata/catalog.json',
type:'get',dataType:'json',async:false,
success:function(data){
$.indexedDB('www.huffmans.cn').transaction('oflb').then(function(){//*READ_ONLY* ,*READ_WRITE* // 0 or 1
console.log("Transaction completed, all data inserted ===");
//loadFromDB("catalog");
}, function(err, e){
console.log("Transaction NOT completed ===", err, e);
},function(transaction){
var oflb = transaction.objectStore('oflb');
$.each(data,function(){
//alert(JSON.stringify(this));
oflb.add(this,this.name);
});
oflb.put({ // is not update
"itemId": 10012,
"name": "ljdd",
"description": "Asd normal looking desktop computer",
"price": 10022,
"rating": 222
},'Desktop');
});},
error:function(xhr,ts,et){alert(et);}
};
$.ajax(setting);
}
function loadFromOflb(table){
$.indexedDB("www.huffmans.cn").objectStore(table).get('Desktop').then(function(data){
alert(JSON.stringify(data));
});
/*
$.indexedDB("www.huffmans.cn").objectStore(table).each(function(elem){
//addRowInHTMLTable(table, elem.key, elem.value);
console.log(elem.key+':'+elem.value);
});
* */
}
function updoflb(table){
$.indexedDB("www.huffmans.cn").objectStore(table).put({
"itemId": 1001,
"name": "lj",
"description": "A normal looking desktop computer",
"price": 100,
"rating": 2
},'Desktop').done(function(result, event){
//result; // Result of the operation. Some operations like delete will return undefined
//event; // Success Event
alert('==');
//if(event){alert('oks');}
}
).fail(function(error, event){
alert(error);
//error; // Type of error that has occured
//event; // Error event
//event.type; // indicates if there was an error or an exception
});
}
function updoflb2(table){
//$.indexedDB("www.huffmans.cn").objectStore(table).delete('Desktop');
//$.indexedDB("www.huffmans.cn").objectStore(table).add('Desktop',{'name':'lij'});
$.indexedDB('www.huffmans.cn').transaction('oflb').then(function(){
console.log("Transaction completed, all data inserted ===");
//loadFromDB("catalog");
}, function(err, e){
console.log("Transaction NOT completed ===", err, e);
},function(transaction){
var oflb = transaction.objectStore('oflb');
//$.each(data,function(){
//alert(JSON.stringify(this));
oflb.put({ // is not update
"itemId": 10012,
"name": "ljdd",
"description": "Asd normal looking desktop computer",
"price": 10022,
"rating": 222
},'lj');
//});
});
/*
$.indexedDB("www.huffmans.cn").objectStore(table).each(function(data){
//alert(JSON.stringify(data));
if(data.key=='Desktop')
data.update({
"itemId": 10012,
"name": "lj",
"description": "Asd normal looking desktop computer",
"price": 10022,
"rating": 222
});
});
* */
}
function usedbidx(){
$.indexedDB('www.huffmans.cn').transaction('oflb').then(function(){//*READ_ONLY* ,*READ_WRITE* // 0 or 1
console.log("Transaction completed, all data inserted ===");
//loadFromDB("catalog");
}, function(err, e){
console.log("Transaction NOT completed ===", err, e);
},function(transaction){
transaction.objectStore('oflb').count().done(function(result,event){alert(result);}); // total is result
//alert($(oflbc).length);
transaction.objectStore('oflb').clear();//.done(function(result,event){alert(result);});
//oflbc = transaction.objectStore('oflb').count();
//alert(transaction.objectStore('oflb').count());
var oflb = transaction.objectStore('oflb').index('name').eachKey(function(item){ //each fn include :k,v
//console.log(item+'<<');//+'<<'+item.key);
});
/*
$.each(data,function(){
//alert(JSON.stringify(this));
oflb.add(this,this.name);
});
oflb.put({ // is not update
"itemId": 10012,
"name": "ljdd",
"description": "Asd normal looking desktop computer",
"price": 10022,
"rating": 222
},'Desktop');
* */
});
}
function deleteDB(){
// Delete the database
$.indexedDB("MyECommerceSite").deleteDatabase();
}
// Download a catalog from the server and save it to the DB
function downloadCatalog(){
$.getJSON("<%=basePath%>github/indexdb/tdata/catalog.json", function(data){
$.indexedDB("MyECommerceSite").transaction("catalog").then(function(){
console.log("Transaction completed, all data inserted");
loadFromDB("catalog");
}, function(err, e){
console.log("Transaction NOT completed", err, e);
}, function(transaction){
var catalog = transaction.objectStore("catalog");
catalog.clear();
$.each(data, function(i){
_(catalog.add(this));
})
});
});
}
// Iterate over each record in a table and display it
function loadFromDB(table){
//emptyTable(table);
_($.indexedDB("MyECommerceSite").objectStore(table).each(function(elem){
//addRowInHTMLTable(table, elem.key, elem.value);
}));
}
function updoflib(table){
$.indexedDB("MyECommerceSite").objectStore(table)
}
// Sort a table based on an index that is setup
function sort(table, key){
//emptyTable(table);
_($.indexedDB("MyECommerceSite").objectStore(table).index(key).each(function(elem){
//addRowInHTMLTable(table, elem.key, elem.value);
}));
}
function emptyDB(table){
_($.indexedDB("MyECommerceSite").objectStore(table).clear());
}
// Read an item from catalog and save it in cart
function addToCart(itemId){
$.indexedDB("MyECommerceSite").objectStore("catalog").get(itemId).then(function(item){
$.indexedDB("MyECommerceSite").objectStore("cart").add(item).done(function(){
loadFromDB("cart");
});
}, function(err, e){
alert("Could not add to cart");
});
}
// Delete an item from cart
function removeFromCart(itemId){
$.indexedDB("MyECommerceSite").objectStore("cart")["delete"](itemId).done(function(){
loadFromDB("cart");
});
}
// Using transactions, read object from cart, add it to wishlist if it does not exist
// and then delete it from the cart. If any operation failes, all these will fail
function moveToWishlist(cartId){
var transaction = $.indexedDB("MyECommerceSite").transaction(["cart", "wishlist"]);
transaction.done(function(){
loadFromDB("cart");
loadFromDB("wishlist");
});
transaction.progress(function(transaction){
transaction.objectStore("cart").get(cartId).done(function(item){
transaction.objectStore("wishlist").add(item).fail(function(){
alert("Already in the wishlist");
}).done(function(){
_(transaction.objectStore("cart")["delete"](cartId));
});
})
});
}
// Read an item from catalog and add it to wishlist
function addToWishlist(itemId){
$.indexedDB("MyECommerceSite").objectStore("catalog").get(itemId).then(function(item){
$.indexedDB("MyECommerceSite").objectStore("wishlist").add(item).done(function(){
loadFromDB("wishlist");
})
}, function(err, e){
alert("Could not add to cart");
});
}
function removeFromWishList(itemId){
$.indexedDB("MyECommerceSite").objectStore("wishlist")["delete"](itemId).done(function(){
loadFromDB("wishlist");
});
}
function renderJSON(val){
var result = [];
for (var key in val) {
result.push("<div class = 'keyval'>");
result.push("<span class = 'key'>", key, "</span>");
result.push("<span class = 'value'>", JSON.stringify(val[key]), "</span>");
result.push("</div>")
}
return result.join("");
}
function _(promise){
promise.then(function(a, e){
console.log("Action completed", e.type, a, e);
}, function(a, e){
console.log("Action completed", a, e);
}, function(a, e){
console.log("Action completed", a, e);
})
}
##########
catalog.json
[{
"itemId": 1001,
"name": "Desktop",
"description": "A normal looking desktop computer",
"price": 100,
"rating": 2
}, {
"itemId": 1002,
"name": "Laptop",
"description": "A laptop computer",
"price": 200,
"rating": 4
}, {
"itemId": 1003,
"name": "Tablet",
"description": "A tablet computer",
"price": 150,
"stock": 4
}, {
"itemId": 1004,
"name": "Embedded",
"description": "Whatever",
"stock": 4
}, {
"itemId": 1005,
"name": "EInk Reader",
"description": "Like a tablet, but different screen",
"price": 150,
"stock": 4
}, {
"itemId": 1006,
"name": "Phone",
"description": "No more a computer",
"price": 250,
"stock": 4
}, {
"itemId": 1007,
"name": "Music players",
"description": "the outer imits ? ",
"price": 10,
"stock": 4
}] |
|