【问题标题】:IndexedDB in a WinJS Binding LIst "undefined"WinJS 绑定列表中的 IndexedDB“未定义”
【发布时间】:2016-01-25 20:39:31
【问题描述】:

好的,所以我创建了我的 IndexedDB,在创建过程中使用“store.put”添加了一些数据。 然后我关闭连接并重新打开连接以使用游标将数据推送到 WinJS 绑定列表:

var myData = new WinJS.Binding.List();
myData.push(cursorp.value);

现在当我“console.log(myData);”我明白了:

[object Object]
myDataStore.js (70,21)
   {
      [functions]: ,
      __proto__: { },
      _binding: undefined,
      _currentKey: 1,
      _keyMap: {
         [functions]: ,
         1: {
            [functions]: ,
            __proto__: { },
            data: {
               [functions]: ,
               __proto__: { },
               theDay: "F",
               id: 1,
               listItemN: "My Note.",
               day: "1/10/2016"
            },
            handle: "1",
            key: "1"
         },
         __proto__: { }
      },
      _keys: [ ],
      _lastNotifyLength: 1,
      _listeners: null,
      _modifyingData: 0,
      _notifyId: 0,
      _pendingNotifications: null,
      _proxy: undefined,
      dataSource: { },
      length: 1,
      onitemchanged: undefined,
      oniteminserted: undefined,
      onitemmoved: undefined,
      onitemmutated: undefined,
      onitemremoved: undefined,
      onreload: undefined
   }

当我尝试执行 ListView 时,我会得到其中包含“未定义”的列表元素。我已经对其进行了更改,以便获得我想要的所有三个项目:

myData.push(cursorp.value.listItemN, cursorp.value.theDay, cursorp.value.day);

但它做同样的事情,每个元素内部都有“未定义”。

我只是没有看到如何从这个绑定列表中提取数据。

这是我正在创建的模板。它通过命名空间从另一个js文件中获取数据的值:

    var myListDataInfo = myOwnNamespce.itemList;

    var myTemp = (function myTemplate(myPromise) {
      return myPromise.then(function(listNote) {
        var div = document.createElement("div");
        div.className = "myListContainer";

        var myListNote = document.createElement("h4");
        myListNote.innerText = listNote.myListDataInfo;
        div.appendChild(myListNote);

        return div;
      })
    });

任何帮助将不胜感激。 -Rob0

【问题讨论】:

  • 我想说的是,对编程的某些方面的绿色/新手会导致这些问题。我已经逐步浏览了代码,发现问题最有可能出在哪里。是时候学习使用 JavaScript 进行异步编程了。稍后再补充。

标签: javascript undefined winjs indexeddb


【解决方案1】:

这就是为什么事情不正常:

  1. 我需要一个回调函数来确保:
var myData = new WinJS.Binding.List();

已处理,然后创建了命名空间:

var myData = new WinJS.Binding.List();

        function loadData(callback) {
          //Open new instance of DB
          var myDataBase = indexedDB.open("notelist");

          myDataBase.onsuccess = function(e) {
            var list = e.target.result.transaction("notes", "readwrite");
            var myStore = list.objectStore("notes");
            myStore.openCursor().onsuccess = function(e) {

              var cursorp = e.target.result;
              if (cursorp) {
                myData.push(cursorp.value);
                cursor.continue();
              } else {
                console.log(myData);
                console.log("Gathered Array!");

                if (typeof callback === "function") {

                  callback();

                }
              };

            };

          };
        };

        function createMyNameSpace() {
            WinJS.Namespace.define('myOwnNamespce', {

              itemList: myData,

            });
        };

为了使回调起作用,我将函数(回调)放在我的 onsuccess 中以创建数据库。

        myDat.onsuccess = function () {
            myLDat = myDat.result;
            loadData(createMyNameSpace);

            console.log("Database initialized!");
        };
  1. 我对模板的有限理解在这里发挥了作用。我发现 this 链接很有帮助。

如果您查看上面的模板代码,您可能会发现我正在尝试获取已通过尝试未定义方法获得的数据。所以模板现在看起来像这样:

    var myListDataInfo = myOwnNamespce.itemList; //This is not needed

    var myTemp = (function myTemplate(myPromise) {
      return myPromise.then(function(listNote) {
        var div = document.createElement("div");
        div.className = "myListContainer";

        var myListNote = document.createElement("h4");
        myListNote.innerText = listNote.data.listItemN; //The change is in this line.
        div.appendChild(myListNote);

        return div;
      })
    });

我还发现 this 文章有助于理解回调。

希望这会有所帮助。

编辑更新

添加 var myData = new WinJS.Binding.List();到代码。会注意到代码在匿名函数内部。

编辑更新

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 2014-11-24
    相关资源
    最近更新 更多