0

我正在尝试创建一个数据管理应用程序,但我使用的是 IndexedDB,而不是基于 Windows 的解决方案或使用 WebSQL。我对它很陌生,但我相信我已经涵盖了这个代码草案的基础。

无论如何,我的问题是,每当我运行代码时,我的 openDB() 函数和 addeventListener() 函数都会在运行时运行并显示在控制台日志上,但是当我尝试运行代码时,据说所有其他函数都是未定义的。问题可能是什么?

在 HTML 文件中,引用了 jQuery 脚本文件。

       (function () {
        var DB_NAME = 'shodex';
        var DB_VERSION = 1;
        var DB_STORE_NAME = 'visitors';
        var db;

        var current_view_pub_key;

        //opens the IndexedDB database   
        function openDb() {
        console.log("open Database......");
        var req = indexedDB.open(DB_NAME, DB_VERSION);
        req.onsuccess = function (evt) {
        db = this.result;
        console.log("Database Opened");
        };
        req.onerror = function (evt) {
        console.error("openDb:", evt.target.errorCode);
        };
        req.onupgradeneeded = function (evt) {
        console.log("Event fired when DB is needed to be upgraded");
        var store = evt.currentTarget.result.createObjectStore(
        DB_STORE_NAME, { keyPath: 'id', autoIncrement: true });
        store.createIndex('name', 'name', { unique: false });
        store.createIndex('date', 'date', { unique: false });
        store.createIndex('whom_to_see', 'whom_to_see', { unique: false });
        store.createIndex('arrival_time', 'arrival_time', { unique: false });
        store.createIndex('reason', 'reason', { unique: false });
        store.createIndex('departure_time', 'departure_time', { unique: false });
        };
          }

        //used to create a transaction
        function getObjectStore(store_name, mode) {
        var tx = db.transaction(store_name, mode);
        return tx.objectStore(store_name);
        }

        //adds a Visitor to the IndexedDB
        function addVisitor(name, date, to_see, arrival_time, reason, departure_time) {
        console.log("Adding the following data to IndexedDB: ", arguments);
        var obj = { name: name, date: date, whom_to_see: to_see, arrival_time: arrival_time, reason: reason, departure_time: departure_time };
        if(typeof blob != undefined)
        {
            obj.blob = blob;
        }
        var store = getObjectStore(DB_STORE_NAME, 'readwrite');
        var req;
        try
        {
            req = store.add(obj);
        }
        catch(e)
        {
            if(e.name == 'DataCloneError')
            displayActionFailure("This engine does not know how to clone a Blob, use Firefox!");
            throw(e);
        }
        req.onsuccess = function (evt) {
            console.log("Insertion into DB was successful. You can heave a huge sigh of relief!");
            displayActionSuccess();
        };
        req.onerror = function () {
            console.error("Insertion into DB failed!");
            displayActionFailure(this.error);
        };
    }

    function displayActionSuccess() {
        alert("Whatever the heck you were doing was successful. Congrats!");
    }

    function displayActionFailure() {
        alert("Oh Oh! System Failure! System Failure!");
    }

     // listens for the submit button event  
    function addEventListeners() {
        console.log("Event Listeners");
        $('#addVisitor').click(function(evt) {
            console.log("Add Visitors Submit button");
            var name = document.getElementsByName("txtName").value;
            var date = document.getElementsByName("txtDate").value;
            var whom_to_see = document.getElementsByName("txtToSee").value;
            var time_of_arrival = document.getElementsByName("txtArrivalTime").value;
            var reason_for_visit = document.getElementsByName("txtReason").value;
            var time_of_departure = document.getElementsByName("timeOfDep");
            addVisitor(name, date, whom_to_see, time_of_arrival, reason_for_visit, time_of_departure);
        });
    }
//makes the database open at runtime
openDb();
addEventListeners();
})
();
4

3 回答 3

0

语法错误 - 最后需要一个右圆括号。即添加

);

于 2013-06-28T12:07:31.793 回答
0

而不是像以前那样引用javascript代码......

我从 javascript 文件中删除了所有其他函数,留下 openDB() 函数并将它们放在主 html 文件中。它自动工作。

于 2013-07-01T22:23:53.883 回答
0

我认为问题在于,当您的匿名函数运行时,浏览器不知道“#addVisitor”元素,因此未创建该元素的单击事件处理程序。

你应该把你的代码放在 "$(function() {" 或 "$(document).ready(function() {" 中:

$(function() {
    (function () {
        var DB_NAME = 'shodex';
        var DB_VERSION = 1;
        ...
于 2013-06-28T21:51:03.570 回答