我創建了一個使用Knockout的master-details ui。主控器上的選定事件處理器綁定給定數據項的詳細信息視圖。一切工作到目前爲止,但我想訪問綁定到詳細信息區域的數據,所以我可以將其發佈到服務器,一旦更新。可以在Knockout中綁定後訪問viewModel嗎?
我是Knockout的新手,所以請告知是否有更好的方法。
//the master binding code
$.ajax({
url: getURL,
success: function (data) {
var viewModel = new itemModel(data);
var scope = document.getElementById("listContainer");
ko.cleanNode(scope);
ko.applyBindings(viewModel, scope);
}
//the viewmodel with event hander
function itemWrapper(item) {
this.SolutionSet = ko.observable(item.SolutionSet);
this.Insight = ko.observable(item.Insight);
this.DateFrom = ko.observable(item.DateFrom);
this.DateTo = ko.observable(item.DateTo);
}
var itemModel = function (data) {
var self = this;
var observableData = ko.utils.arrayMap(data, function (item) {
return new itemWrapper(item);
});
self.items = ko.observableArray(observableData);
onItemSelected = function (data) {
var scope = document.getElementById("itemEditor");
ko.cleanNode(scope);
ko.applyBindings(data, scope);
};
}