Search code examples
javascriptundefinedwinjsindexeddb

IndexedDB in a WinJS Binding LIst "undefined"


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:

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

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

[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:

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:

    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


Solution

  • This is why things were not working:

    1. I needed a callback function to make sure that:
    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.

            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.

    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.

    EDIT UPDATE