鉴于这个小提琴,我怎样才能正确地用 dummyResults 填充 searchResult 和用 dummyItems 填充 visibleItem?
http://jsfiddle.net/scottbeeson/SRUKN/
var dummyResults = [
{ name: "Server1", type: "Server" },
{ name: "Server2", type: "Server" },
{ name: "Server3", type: "Server" },
{ name: "App1", type: "Application" },
{ name: "App2", type: "Application" },
{ name: "App3", type: "Application" }
]
var dummyItems = [
{ name: "Server1", type: "Server" }
]
function visibleItem() {
var self = this;
this.name = ko.observable("");
this.type = ko.observable("");
this.description = ko.observable("");
}
function searchResult() {
var self = this;
this.name = ko.observable("");
this.type = ko.observable("");
}
var viewModel = {
searchResult: searchResult(),
visibleItem: visibleItem(),
showItem: function (item) {
visibleItems.push(item);
}
}
$(document).function() {
ko.mapping.fromJS(dummyResults, searchResult);
ko.mapping.fromJS(dummyItems, visibleItem);
ko.applyBindings(viewModel);
tmp = ko.toJS(viewModel);
}