WinJS绑定中的IndexedDB LIst“undefined”

时间:2022-08-13 22:51:09

Ok so I've created my IndexedDB added some data during creation with 'store.put'. I then close the connection and reopen a connection to use a cursor to push the data to a WinJS Binding List:

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

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

Now when I "console.log(myData);" I get this:

现在当我“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
   }

When I try to do a ListView I get the list element with "undefined" inside of it. I have changed it so that I get all three items that I want with this:

当我尝试做ListView时,我得到了里面带有“undefined”的list元素。我已经改变它,以便我得到我想要的所有三个项目:

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

But it does the same thing, each element has "undefined" inside of it.

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

I am just not seeing how to pull the data from this binding list.

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

This is the template that I am creating. It gets the value of the data from another js file through a namespace:

这是我正在创建的模板。它通过命名空间从另一个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;
      })
    });

Any help would be appreciated. -Rob0

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

1 个解决方案

#1


0  

This is why things were not working:

这就是事情不起作用的原因:

  1. I needed a callback function to make sure that:
  2. 我需要一个回调函数来确保:

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

was processed and then the namespace created:

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

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,

            });
        };

To make the callback work I put the function(callback) inside of my onsuccess for the database creation.

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

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

            console.log("Database initialized!");
        };
  1. My limited understanding of the template was at play here. I Found this link to be helpful.
  2. 我对模板的有限理解在这里发挥作用。我发现这个链接很有帮助。

If you look at the code above for the template you may see what I was doing was trying get data that had already been gotten by trying an undefined method. So the template now looks like 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;
      })
    });

I also found this article helpful in understanding callbacks.

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

Hope this helps.

希望这可以帮助。

EDIT UPDATE

Added var myData = new WinJS.Binding.List(); to the code. Will note that the code is inside of an anonymous function.

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

EDIT UPDATE

#1


0  

This is why things were not working:

这就是事情不起作用的原因:

  1. I needed a callback function to make sure that:
  2. 我需要一个回调函数来确保:

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

was processed and then the namespace created:

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

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,

            });
        };

To make the callback work I put the function(callback) inside of my onsuccess for the database creation.

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

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

            console.log("Database initialized!");
        };
  1. My limited understanding of the template was at play here. I Found this link to be helpful.
  2. 我对模板的有限理解在这里发挥作用。我发现这个链接很有帮助。

If you look at the code above for the template you may see what I was doing was trying get data that had already been gotten by trying an undefined method. So the template now looks like 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;
      })
    });

I also found this article helpful in understanding callbacks.

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

Hope this helps.

希望这可以帮助。

EDIT UPDATE

Added var myData = new WinJS.Binding.List(); to the code. Will note that the code is inside of an anonymous function.

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

EDIT UPDATE