2015-02-24 34 views
0

我的页面中有一个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相当大。换句话说,我在multiselect中没有选定的值。观察对象的值在页面上是正确的 - 我可以输出spm.locations的文本,这是正确的。我尝试过使用ratelimit,但这似乎也不奏效。我甚至试着让这两个Ajax调用同步,这没有帮助。我觉得秘密筹码是在限制利率,但是如何让一个可观察的等待不同的观察者来填充?

回答

2

您可以订阅您观察到的阵列变化

self.spm.locations.subscribe(function (newValue) { 
    if (newValue && newValue.length > 0) { 
     // newValue is set, we can populate our locations_list now 
    } 
}, this) 

BTW你不想推值的observableArray一个接一个,将其推入临时数组,看到这个post

+0

不熟悉认购 - 在阅读文档时,我是不是需要订阅locations_list?我希望等待所有位置在选择多选中的位置之前从服务器完成加载?我是否错误地思考它? – ghiotion 2015-02-24 17:48:43

+0

我想我明白了。使用notifyWhenChangesStop扩展locations_list,然后订阅locations_list并仅在订阅中加载选定的数据。谢谢Max。 – ghiotion 2015-02-24 18:12:40

+0

@max是否可以订阅可观察数组?任何地方的任何文件?谢谢 。当我们推动一些新的订阅会触发,但当我改变内部observableArray内的东西,即可观察的值它不会被解雇。 ?任何想法 。 – 2015-02-25 09:00:20