敲除将新行添加到记录映射插件列表
Knockout add new row to list of records mapping plugin
我正在使用映射插件更新视图模型,如下所示:
$.ajax({
type: "POST",
url: url,
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (json) {
if (json.Page == 1) {
ko.mapping.fromJS(json, {}, self);
} else {
self.List().push(ko.mapping.fromJS(json.List));
}
console.log(ko.toJSON(self.List()));
},
});
然而,它没有更新 List() 可观察数组(在 ajax 调用完成时仅添加新项目 - 对于第 2 页(数据库中的下几个项目),它添加了一个新列表 => 注意“[ " 控制台输出中的元素:
AJAX 响应 (json)
{"DiscountType":"Transaction","List":[{"ActivationDate":"/Date(1427215761818)/","CustomerName":"Another two 2","CustomerNumber":4328,"Percent":20,"HasDiscount":true},{"ActivationDate":"/Date(1428079761818)/","CustomerName": "Another tree","CustomerNumber":1212,"Percent":20,"HasDiscount":true}],"Page":2}
控制台输出:
[{"ActivationDate":"/Date(1388556000000)/","CustomerName":"Test1 Inc.","CustomerNumber":10032,"Percent":20, "HasDiscount":true},{"ActivationDate":"/日期(1426783761818)/","CustomerName":"Another One 1","CustomerNumber":5174,"Percent" :20,"HasDiscount":true},[{"ActivationDate":"/日期(1427215761818)/","CustomerName":"Another two 2","CustomerNumber":4328,"Percent":20,"HasDiscount":true},{"ActivationDate":"/日期(1428079761818)/","CustomerName":"Another tree","CustomerNumber": 1212,"Percent":20,"HasDiscount":真}]]
我做错了什么?
要推送返回数组的各个元素,请执行类似
的操作
success: function (json) {
if (json.Page == 1) {
ko.mapping.fromJS(json, {}, self);
}
else {
json.List.forEach(
function(element) {
self.List.push( ko.mapping.fromJS(element) );
} );
}
console.log(ko.toJSON(self.List()));
}
var vm = function() {
var self = this;
self.List = ko.observableArray([]);
self.initiate = function() {
json = JSON.parse('{"DiscountType":"Transaction","List":[{"ActivationDate":"/Date(1427215761818)/","CustomerName":"Another two 2","CustomerNumber":4328,"Percent":20,"HasDiscount":true},{"ActivationDate":"/Date(1428079761818)/","CustomerName":"Another tree","CustomerNumber":1212,"Percent":20,"HasDiscount":true}],"Page":2}');
json.List.forEach(function(entry) {
self.List.push(ko.mapping.fromJS(entry));
});
};
};
vm = new vm();
ko.applyBindings(vm);
vm.initiate();
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/knockout.mapping/2.4.1/knockout.mapping.min.js'></script>
<html>
<div data-bind='foreach: List'>
<div data-bind='text: ActivationDate'></div>
</div>
</html>
我正在使用映射插件更新视图模型,如下所示:
$.ajax({
type: "POST",
url: url,
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (json) {
if (json.Page == 1) {
ko.mapping.fromJS(json, {}, self);
} else {
self.List().push(ko.mapping.fromJS(json.List));
}
console.log(ko.toJSON(self.List()));
},
});
然而,它没有更新 List() 可观察数组(在 ajax 调用完成时仅添加新项目 - 对于第 2 页(数据库中的下几个项目),它添加了一个新列表 => 注意“[ " 控制台输出中的元素:
AJAX 响应 (json)
{"DiscountType":"Transaction","List":[{"ActivationDate":"/Date(1427215761818)/","CustomerName":"Another two 2","CustomerNumber":4328,"Percent":20,"HasDiscount":true},{"ActivationDate":"/Date(1428079761818)/","CustomerName": "Another tree","CustomerNumber":1212,"Percent":20,"HasDiscount":true}],"Page":2}
控制台输出:
[{"ActivationDate":"/Date(1388556000000)/","CustomerName":"Test1 Inc.","CustomerNumber":10032,"Percent":20, "HasDiscount":true},{"ActivationDate":"/日期(1426783761818)/","CustomerName":"Another One 1","CustomerNumber":5174,"Percent" :20,"HasDiscount":true},[{"ActivationDate":"/日期(1427215761818)/","CustomerName":"Another two 2","CustomerNumber":4328,"Percent":20,"HasDiscount":true},{"ActivationDate":"/日期(1428079761818)/","CustomerName":"Another tree","CustomerNumber": 1212,"Percent":20,"HasDiscount":真}]]
我做错了什么?
要推送返回数组的各个元素,请执行类似
的操作success: function (json) {
if (json.Page == 1) {
ko.mapping.fromJS(json, {}, self);
}
else {
json.List.forEach(
function(element) {
self.List.push( ko.mapping.fromJS(element) );
} );
}
console.log(ko.toJSON(self.List()));
}
var vm = function() {
var self = this;
self.List = ko.observableArray([]);
self.initiate = function() {
json = JSON.parse('{"DiscountType":"Transaction","List":[{"ActivationDate":"/Date(1427215761818)/","CustomerName":"Another two 2","CustomerNumber":4328,"Percent":20,"HasDiscount":true},{"ActivationDate":"/Date(1428079761818)/","CustomerName":"Another tree","CustomerNumber":1212,"Percent":20,"HasDiscount":true}],"Page":2}');
json.List.forEach(function(entry) {
self.List.push(ko.mapping.fromJS(entry));
});
};
};
vm = new vm();
ko.applyBindings(vm);
vm.initiate();
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/knockout.mapping/2.4.1/knockout.mapping.min.js'></script>
<html>
<div data-bind='foreach: List'>
<div data-bind='text: ActivationDate'></div>
</div>
</html>