我需要<select />
通过两个不同的 AJAX 调用来获取 a 的当前值和可用选项(由于几个原因,我无法对它们进行排队/链接)。
我不知道这是否重要,但读取当前值的调用在每次读取可用选项之前完成。
使用 Knockout 2.0.0 和 Knockout Mapping 2.0.3 我用 绑定了选择data-bind="options: values, value: valueComputed"
,但我发现 Knockout 正在删除/忽略第一个 AJAX 调用设置的值,直到获取可用选项的调用完成才能设置当前值。
它是否正确?是否可以告诉 Knockout “这是当前值,当可用选项可用时选择它”?
经过几次测试后,我想出了一个问题:我没有将普通的 observable 绑定到 select 的值,而是使用了计算的 observable,我截取了该值,并且如果新值未定义,则不更改底层的 observable。
我在做坏事吗?
一个 jsFiddle 示例:http: //jsfiddle.net/KeNUU/
我正在使用的 JavaScript 代码:
var viewModel = function () {
var self = this;
// The underlying observable where
// the selected value is stored.
self.value = ko.observable();
// The observable bound to
// the value of the drop down.
self.values = ko.mapping.fromJS([]);
// Use a computed observable to intercept the undefined
// value placed by KnockOut when binding the drop down.
self.valueComputed = ko.computed({
"read": function () {
return ko.utils.unwrapObservable(self.value);
},
"write": function (value) {
// Update the underlying observable only when
// there is a value, if it's undefined ignore it.
if (value) {
self.value(value);
}
}
});
// Simulate the AJAX request which fetches the actual value,
// this request must complete before the second one.
setTimeout(function () {
self.valueComputed("b");
}, 1000 * 1);
// Simulate the AJAX request which fetches the available values,
// this reqest must complete after the first one.
setTimeout(function () {
ko.mapping.fromJS(["a", "b", "c", "d"], {}, self.values);
}, 1000 * 2);
};
$(document).ready(function () {
ko.applyBindings(new viewModel());
});