面对常见的“绑定问题”。下面是视图模型
function DetailListViewModel() {
this.details = new Details();
this.productdetails = ko.observableArray([]);
this.show = function (item) {
$.getJSON("Products.json", {}, function (data) {
this.productdetails.push(this.details.init(data));
});
};
}
function Details() {
this.author = ko.observable();
this.text = ko.observable();
this.init = function (temp) {
return {
author: temp.Author,
text: temp.Text
};
};
}
var TaskListViewModel = {
tasks: ko.observableArray([]),
addTask: function () {
self.tasks.push(new Task({ BomID: this.BomID() }, { CreatedBy: this.CreatedBy() }));
},
ShowProductInfo: function (item) {
DetailListViewModel.show(item);
}
};
$(function () {
$.getJSON("Tasks.json", function (allData) {
TaskListViewModel.tasks.push(toKoObserable($.parseJSON(allData.GetDataResult).RESULT.DOCUMENT));
ko.applyBindings(TaskListViewModel);
var DetailListViewModel = new DetailListViewModel();
ko.applyBindings(DetailListViewModel);
});
function toKoObserable(blog) {
return {
BomID: ko.observable(blog[0].BOMID),
CreatedBy: ko.observable(blog[0].CreatedBy)
};
}
});
我收到此错误
Uncaught Error: Unable to parse bindings.
Message: ReferenceError: author is not defined;
Bindings value: text:author
我的 HTML 是:
<h3 data-bind="text:author">/h3>