我正在使用 Windows 8.1 winJS,并尝试从 indexedb 填充 listView 并且无法使其工作。我从 jsonStr 第 76 行得到一个字符串,但我无法绑定到 html。
我的 javascript
(function () {
"use strict";
var nav = WinJS.Navigation;
var session = WinJS.Application.sessionState;
var util = WinJS.Utilities;
WinJS.Namespace.define("DataExampleA",
{
itemList: new WinJS.Binding.List()
})
var plantsData1 = [
{ id: 1, title: "Banana blast1", text: "Ice cream", picture: "images/plants/Acalypha pendula.jpg" },
{ id: 2, title: "Banana blast2", text: "Ice cream", picture: "images/plants/Acalypha pendula.jpg" },
{ id: 3, title: "Banana blast3", text: "Ice cream", picture: "images/plants/Acalypha pendula.jpg" }
];
var indexedDB = window.msIndexedDB;
var IDBTransaction = window.IDBTransaction;
if (!window.indexedDB) {
window.console.log("Your browser doesn't support a stable version of IndexedDB.")
}
var db;
var request = window.msIndexedDB.open("plantsDatabase", 1);
request.onerror = function (event) {
console.log("error: ");
};
request.onsuccess = function (event) {
db = request.result;
console.log("success: " + db);
};
request.onupgradeneeded = function (event) {
console.log('Creating object stores');
var db = event.target.result;
var objectStore = db.createObjectStore("plantsStore", { keyPath: "id" });
for (var i in plantsData1) {
objectStore.add(plantsData1[i]);
}
}
function read() {
var transaction = db.transaction(["plantsStore"]);
var objectStore = transaction.objectStore("plantsStore");
var request = objectStore.get("00-03");
request.onerror = function (event) {
console.log("Unable to retrieve daa from database!");
};
request.onsuccess = function (event) {
// Do something with the request.result!
if (request.result) {
// console.log("Name: " + request.result.name + ", Age: " + request.result.age + ", Email: " + request.result.email);
} else {
console.log("Kenny couldn't be found in your database!");
}
};
}
function buttonreadAll(plantsBind) {
var transaction = db.transaction(["plantsStore"]);
var objectStore = transaction.objectStore("plantsStore");
objectStore.openCursor().onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
var plantsBind = cursor.value;
var jsonStr = JSON.stringify(plantsBind);
console.log(jsonStr);
var dataList = new WinJS.Binding.List(cursor.value);
// Create a namespace to make the data publicly accessible.
var publicMembers =
{
itemList: dataList
};
WinJS.Namespace.define("DataExampleA", publicMembers);
cursor.continue();
}
else {
console.log("No more entries!");
}
};
}
WinJS.UI.Pages.define("/pages/hub/hub.html", {
processed: function (element) {
return WinJS.Resources.processAll(element);
},
// This function is called whenever a user navigates to this page. It
// populates the page elements with the app's data.
ready: function (element, options) {
var readAll = document.getElementById("readAll");
readAll.addEventListener("click", buttonreadAll, false);
},
unload: function () {
// TODO: Respond to navigations away from this page.
},
updateLayout: function (element) {
/// <param name="element" domElement="true" />
// TODO: Respond to changes in layout.
},
});
})();
和我的html
<div class="section3" data-win-control="WinJS.UI.HubSection" data-win-options="{ isHeaderStatic: true }" data-win-res="{ winControl: {'header': 'Section3'} }">
<div id="IconTextTemplate1" class="win-container win-itembox win-item" data-win-control="WinJS.Binding.Template">
<div style=" padding: 5px; overflow: hidden; display: -ms-grid;">
<img data-win-bind="alt: title; src: picture" src="#" style="width: 80%; margin-left: 10%; margin-right:10%; margin-bottom:2%; -ms-grid-column: 1;" />
<div style="margin: 15px; -ms-grid-row: 2">
<h3 data-win-bind="innerText: title"></h3>
<h3 data-win-bind="innerText: picture"></h3>
<h6 data-win-bind="innerText: text"></h6>
</div>
</div>
</div>
<div id="basicListView" data-win-control="WinJS.UI.ListView"
data-win-options="{
itemDataSource : DataExampleA.itemList.dataSource,
itemTemplate: select('#IconTextTemplate1'),
layout : {type: WinJS.UI.GridLayout}}">
</div>
</div>