gpt4 book ai didi

javascript - 为什么 Knockout.js 数据绑定(bind)到 checkValue 会覆盖 observable?

转载 作者:行者123 更新时间:2023-11-28 12:28:04 25 4
gpt4 key购买 nike

我在使用 knockout 制图插件时遇到问题。我有一个 ShippingOptions 数组,我使用选中的值将其数据绑定(bind)到单选按钮,并将选中的属性设置为我的 View 模型上名为 ShippingChoice 的另一个对象。

我有一个计算出的可观察总计,它是我的 SubTotalShippingChoice.Price 的总和

当选择其中一个单选选项时,它会正确地将 ShippingChoice 设置为所选的运输选项,但会导致 ShippingChoice 不再是可观察的并破坏我的计算总数

控制台显示错误“类型错误:数字不是函数”,因为我正在计算中访问ShippingChoice().Price。一旦设置了该值,它似乎就会用一个普通对象覆盖我的可观察值。

我尝试使用自定义映射将运输选择设置为可观察的,但这没有帮助。

Jsfiddle 演示地址: http://jsfiddle.net/on3al/2dv33vor/9/

任何见解或建议将不胜感激。我已经为这个问题苦恼太久了。

HTML

      <!-- ko foreach: ShippingOptions -->
<div class="radio">
<input type="radio" name="optionsRadios"
data-bind="checkedValue: $data ,checked: $root.ShippingChoice" />
<span data-bind="text: Carrier"></span>
<span data-bind="text: Service"></span>
<span data-bind="money: Price"></span>
</div>
<!-- /ko -->
</div>
<div class="col-xs-4">
<h5 class="text-right">Subtotal <strong data-bind="money: SubTotal"></strong></h5>
<h5 class="text-right" data-bind="money: ShippingChoice().Price">Shipping </h5>
<h4 class="text-right">Total <strong data-bind="money: Total"></strong></h4>
</div>

Javascript

   var mapping = {
'ShippingChoice': {
create: function (options) {
return ko.observable(options.data);
}
}
};

CartViewModel = function (data) {
var self = this;
ko.mapping.fromJS(data, mapping, self);

self.SubTotal = ko.computed(function () {
var subTotal = 0;
for (var i = 0; i < self.Items().length; i++) {
var item = self.Items()[i];
subTotal += item.Quantity() * item.Product.Price();
}
return subTotal;
}, self);

self.Total = ko.computed(function () {
return self.SubTotal()+ self.ShippingChoice().Price;
}, self);

self.decreaseQty = function (item) {
var currQty = item.Quantity();
if (currQty > 0) {
item.Quantity(currQty - 1);
}
self.updateQty(item);
};

self.increaseQty = function (item) {
var currQty = item.Quantity();
item.Quantity(currQty + 1);
self.updateQty(item);
};
};

ko.bindingHandlers.money = {
update: function (element, valueAccessor, allBindingsAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
var positions = 2;
var formattedValue = value.toFixed(positions);
var finalFormatted = '$' + formattedValue;

ko.bindingHandlers.text.update(element, function () {
return finalFormatted;
});
},
defaultPositions: 2,
};

var cartViewModel = new CartViewModel({
"$id": "1",
"Id": "540eb73cff622605c4f45b39",
"Items": [{
"$id": "2",
"Product": {
"$id": "3",
"Status": "In Stock",
"Price": 19.99,
"QuantityInStock": 12,
"Brand": "Dummy Brand",
"Weight": 8.0,
"Width": 12.0,
"Length": 14.0,
"Height": 3.0,
"UrlSlug": "dummy-slug",
"Title": "Dummy Product",
},
"Quantity": 7,
}],
"Country": "CA",
"PostalCode": null,
"ShippingOptions": [{
"$id": "4",
"Carrier": "USPS",
"Price": 22.1,
"Service": "FirstClassPackageInternationalService"
}, {
"$id": "5",
"Carrier": "USPS",
"Price": 32.85,
"Service": "PriorityMailInternational"
}, {
"$id": "6",
"Carrier": "USPS",
"Price": 46.96,
"Service": "ExpressMailInternational"
}],
"ShippingChoice": {
"$id": "7",
"Carrier": "",
"Price": 8.0,
"Service": ""
}
});
ko.applyBindings(cartViewModel);

最佳答案

您还可以通过在映射选项中“复制”您的 ShippingOptions 来解决您的问题:

var mapping = {
'ShippingChoice': {
create: function (options) {
return ko.observable(options.data);
}
},
copy: [ 'ShippingOptions' ]
};

这将产生普通对象数组,并且价格将是数字而不是可观察值。

http://jsfiddle.net/2dv33vor/12/

关于javascript - 为什么 Knockout.js 数据绑定(bind)到 checkValue 会覆盖 observable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25779559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com