下面的代码在我第一次调用时工作正常,但是在第二次调用中,gridviewmodel 变得混乱,所有数据都变得多余
$.ajax({
url: 'rest service uri',
type: 'GET',
data: parameters,
dataType: 'json',
crossDomain: true,
success: function (data) {
function gridviewModel() {
var self = this;
self.items = ko.mapping.fromJS(data);
self.columns = [
new column("Name", "name", "Asc"),
new column("URL", "name", "Asc"),
new column("Embargo Date", "name", "Asc"),
new column("Status", "name", "Asc"),
new column("Reviewer Name", "name", "Asc"),
new column("Approver Name", "name", "Asc")
];
};
ko.applyBindings(new gridviewModel());
}
我尝试使用 removeAll() 清除可观察数组,但它不适用于我。
任何想法?
只是对于任何人将来可能会遇到这个问题,我设法通过在 Knockout 映射插件中调用 ko.mapping.fromJS(data, gridviewModel.items) 来修复它完整代码是
var gridviewModel = {
items: ko.mapping.fromJS([]),
columns: [
new column("Name", "name", "Asc"),
new column("URL", "name", "Asc"),
new column("Embargo Date", "name", "Asc"),
new column("Status", "name", "Asc"),
new column("Reviewer Name", "name", "Asc"),
new column("Approver Name", "name", "Asc")
]
};
$(function () {
ko.applyBindings(gridviewModel, document.getElementById("wip"));
});
希望这可以解决其他问题。