我的页面中有一个kendoMultiSelect小部件,它从JSON调用中获取它的值,并从单独的JSON调用中获取所选值。
<select id="locations" data-bind="kendoMultiSelect: {data: locations_list, value: spm.locations}" class="form-control"></select>
function SchedulerProfileModel(){
var self = this;
self.locations = ko.observableArray();
}
function SchedulerProfile(params) {
var self = this;
self.locations_list = ko.observableArray(['--------']);
}
function MakeAjaxCalls(){
$.ajax({
url: '/api/location',
data: {"q": JSON.stringify(filters)},
dataType: "json",
contentType: "application/json",
success: function(data) {
for (var i = data.objects.length - 1; i >= 0; i--) {
self.locations_list.push(data.objects[i].name+": "+data.objects[i].state_province);
};
}
});
}
function CurrentSchedulerProfile() {
//does the current user already have a shceduler profile?
$.ajax({
url: '/api/schedulerprofile',
data: "q=" + JSON.stringify({"single":true,"filters":[{"name": "user_id", "op": "eq", "val": self.user_id()}]}),
dataType: "json",
contentType: "application/json",
success: function(data) {
self.requesttype = "put";
for (var i = data.locations.length - 1; i >= 0; i--) {
self.spm.locations.push(data.locations[i].name+": "+data.locations[i].state_province);
};
});
}
$.when(MakeAjaxCalls()).then(CurrentSchedulerProfile);
这在开发中运行良好 - 一直都很好。问题是,当我部署到生产时,locations_list observable似乎在spm.locations observable之前没有填充。 locations_list非常大。换句话说,我在多选中没有选定的值。可观察量的值在页面上是正确的 - 我可以输出spm.locations的文本,它是正确的。我尝试过使用ratelimit,但这似乎也不起作用。我甚至尝试使两个ajax调用同步,但没有帮助。我觉得秘密酱是在速率限制,但是有什么方法可以让一个观察者等待不同的观察者来填充?
答案 0 :(得分:2)
您可以订阅您的可观察数组更改
self.spm.locations.subscribe(function (newValue) {
if (newValue && newValue.length > 0) {
// newValue is set, we can populate our locations_list now
}
}, this)
顺便说一句,您不希望逐个推送值observableArray
,将其推入临时数组,请参阅post