我有一些简化的代码可以帮助在客户端执行一些计算。数据将在服务器端验证。但是,我注意到可以使用chrome开发者控制台以新值运行knockout observable。考虑这个例子:
var ViewModel = function() {
var self = this;
self.GuestCounts = ko.observableArray([0, 1, 2, 3])
self.GuestCount = ko.observable();
self.PricePerGuest = ko.observable(10); // Retrieved from server and validated on server
}
ko.applyBindings(ViewModel);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
RSVP For <select data-bind="options: GuestCounts(), value: GuestCount"></select>
Total Price: <span data-bind="text: PricePerGuest() * GuestCount()"></span>
如何阻止某人打开开发者控制台,并输入self.PricePerGuest(1)
或self.Guests(10000)
(注意:这似乎不适用于Stack Overflow片段创建者)