将静态json文件放入indexedDB

时间:2017-06-21 14:52:53

标签: javascript json angular storage indexeddb

我正在尝试存储来自json文件存储服务器端的数据,我正在使用角度js和html,我可以通过在我的indexeddb函数中输入它来添加json数据,但是我需要使用http.get和然后把它放在数据库中,这是我的代码。

$scope.save_data = function save_data(event) {

let indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.shimIndexedDB;

let open = indexedDB.open("myDB", 1);

open.onupgradeneeded = function () {

    let db = open.result;
    let store = db.createObjectStore("the_Times", { keyPath: "id", value: "Article1" });
    let store1 = db.createObjectStore("the_Times1", { keyPath: "id1", value: "Article2" });
    let store2 = db.createObjectStore("the_Times2", { keyPath: "id2", value: "Article3" });
    let index = store.createIndex("Article1", ["news.Article1"]);
    let index1 = store1.createIndex("Article2", ["news.Article2"]);
    let index2 = store2.createIndex("Article3", ["news.Article3"]);

    open.onsuccess = function (e) {
        let db = open.result;
        let tx = db.transaction("the_Times", "readwrite");
        let tx1 = db.transaction("the_Times1", "readwrite");
        let tx2 = db.transaction("the_Times2", "readwrite");
        let store = tx.objectStore("the_Times");
        let store1 = tx1.objectStore("the_Times1");
        let store2 = tx2.objectStore("the_Times2");
        let index = store.index("Article1");
        let index1 = store1.index("Article2");
        let index2 = store2.index("Article3");
        console.log('added successfully');

        //this is where i want to pull data from a url containing the json data and put it in the DB
        store.put({"JSON string is here... Should be url json data"});
        store1.put({"JSON string is here"});
        store2.put({"JSON string is here"});

        let transaction = db.transaction(["the_Times"], "readonly");
        let objectStore = transaction.objectStore("the_Times", "the_Times1", "the_Times2");
        let cursor = objectStore.openCursor();

        cursor.onsuccess = function (e) {
            let res = e.target.result;
            if (res) {
                console.log(res.key);
                console.dir(res.value);
                res.continue();
            }
            tx.oncomplete = function () {
                db.open();
            };
        }
    }
}
};

0 个答案:

没有答案