下拉列表未从计算的可观察值更新
dropdown not updated from computed observable
我有以下计算可观察值:
self.getMovieDates = ko.computed(function() {
$.ajax({
url: '/Utilities/UpdateTimesDropdown',
data: { apiId: self.movieSelectedValue(), locationId: self.theatreSelectedValue() },
dataType: 'jsonp',
success: function (data) {
console.log(data.showtimes);
return data.showtimes;
}
});
}, self);
html 看起来像这样:
<select id="DateDD" data-bind="options: getMovieDates,
optionsText: 'title',
optionsValue: 'id',
optionsCaption: 'Select Showdate',
value: dateSelected"></select>
我可以在 firebug 中看到正在发出 ajax 请求,我正在接收值。 "console.log(data.showtimes)" 看起来像这样:
[Object { id="3/24/2015", title="Today (Tue, Mar 24, 2015)"}, Object { id="3/25/2015", title="Tomorrow (Wed, Mar 25, 2015)"}, Object { id="3/26/2015", title="Thu, Mar 26, 2015"}]
不幸的是,下拉列表没有填充,我 运行 缺乏想法,有什么想法吗?
您的成功函数正在将数据返回到调用它的 jquery 内部,而不是您传递给计算的外部函数。
您必须创建一个可观察对象,然后将其设置到您的成功函数中。然后您在视图模型中使用您的可观察对象,而不是您的计算对象。
编辑:您自己的帮助 link:https://github.com/knockout/knockout/wiki/Asynchronous-Dependent-Observables :)
我有以下计算可观察值:
self.getMovieDates = ko.computed(function() {
$.ajax({
url: '/Utilities/UpdateTimesDropdown',
data: { apiId: self.movieSelectedValue(), locationId: self.theatreSelectedValue() },
dataType: 'jsonp',
success: function (data) {
console.log(data.showtimes);
return data.showtimes;
}
});
}, self);
html 看起来像这样:
<select id="DateDD" data-bind="options: getMovieDates,
optionsText: 'title',
optionsValue: 'id',
optionsCaption: 'Select Showdate',
value: dateSelected"></select>
我可以在 firebug 中看到正在发出 ajax 请求,我正在接收值。 "console.log(data.showtimes)" 看起来像这样:
[Object { id="3/24/2015", title="Today (Tue, Mar 24, 2015)"}, Object { id="3/25/2015", title="Tomorrow (Wed, Mar 25, 2015)"}, Object { id="3/26/2015", title="Thu, Mar 26, 2015"}]
不幸的是,下拉列表没有填充,我 运行 缺乏想法,有什么想法吗?
您的成功函数正在将数据返回到调用它的 jquery 内部,而不是您传递给计算的外部函数。
您必须创建一个可观察对象,然后将其设置到您的成功函数中。然后您在视图模型中使用您的可观察对象,而不是您的计算对象。
编辑:您自己的帮助 link:https://github.com/knockout/knockout/wiki/Asynchronous-Dependent-Observables :)