这个函数在做什么?我对读写部分最困惑
What is this function doing? i'm most confused with the Read and write part
我知道这是一个构造函数,但我不知道它的读写部分。我知道它有一个 if 和 else 语句,但我对它的作用感到困惑!
self.CondInspecChks_RevValve_UI = ko.computed({
read: function () {
return self.CondInspecChks_RevValve() == 1 ? true : false;
},
write: function (newValue) {
self.CondInspecChks_RevValve(newValue ? 1 : 0);
}
});
这是一个Knockout computed observable - it allows a dynamic value to be assigned to a KO observable。
const normalObservable = ko.observable("hello");
const computedObservable = ko.computed(function() {
return "my dynamic value is: " + normalObservable();
})
console.log(normalObservable())
console.log(computedObservable())
//update the observable
normalObservable("world");
console.log(normalObservable())
//the computed also changed
console.log(computedObservable())
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
在这种特殊情况下,这是使用计算的高级构造使其成为 writeable computed。
const someWriteableValue = ko.observable("hello")
const computed = ko.computed({
read: function () {
return "my dynamic value is: " + someWriteableValue();
},
write: function (value) {
someWriteableValue(value);
}
})
console.log(computed());
//update the computed
computed("world");
console.log(computed());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
这是一个淘汰赛computed observable which is writable。
它依赖于存在一个名为 CondInspecChks_RevValve
的底层可观察对象 属性,它是数字(1 或 0),以及问题中的可观察对象 CondInspecChks_RevValve_UI
returns true
或 false
并允许您写入它,这将根据需要更新基础 属性。
我知道这是一个构造函数,但我不知道它的读写部分。我知道它有一个 if 和 else 语句,但我对它的作用感到困惑!
self.CondInspecChks_RevValve_UI = ko.computed({
read: function () {
return self.CondInspecChks_RevValve() == 1 ? true : false;
},
write: function (newValue) {
self.CondInspecChks_RevValve(newValue ? 1 : 0);
}
});
这是一个Knockout computed observable - it allows a dynamic value to be assigned to a KO observable。
const normalObservable = ko.observable("hello");
const computedObservable = ko.computed(function() {
return "my dynamic value is: " + normalObservable();
})
console.log(normalObservable())
console.log(computedObservable())
//update the observable
normalObservable("world");
console.log(normalObservable())
//the computed also changed
console.log(computedObservable())
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
在这种特殊情况下,这是使用计算的高级构造使其成为 writeable computed。
const someWriteableValue = ko.observable("hello")
const computed = ko.computed({
read: function () {
return "my dynamic value is: " + someWriteableValue();
},
write: function (value) {
someWriteableValue(value);
}
})
console.log(computed());
//update the computed
computed("world");
console.log(computed());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
这是一个淘汰赛computed observable which is writable。
它依赖于存在一个名为 CondInspecChks_RevValve
的底层可观察对象 属性,它是数字(1 或 0),以及问题中的可观察对象 CondInspecChks_RevValve_UI
returns true
或 false
并允许您写入它,这将根据需要更新基础 属性。