从$getJSON加载下拉值
loading dropdown value from $getJSON
我有一个带有下拉列表的表单,页面使用Knockout JS将数据绑定到html元素。我有下面的代码来预填充数据库中的值。
JS-
self.Irr = ko.observableArray([]);
self.Crp = ko.observableArray([]);
self.Incr = ko.observableArray([]);
self.Brnd = ko.observableArray([]);
self.Sec4 = ko.observableArray([]);
$.getJSON("GetIrr", null, function (data) { self.Irr(data); })
.done(function () {
$.getJSON("GetCrp", null, function (data) { self.Crp(data); });
})
.done(function () {
$.getJSON("GetTill", null, function (data) { self.Sec4(data); });
})
.done(function () {
$.getJSON("GetBrnd", null, function (data) { self.Brnd(data); });
})
.done(function () {
$.getJSON("GetIncr", null, function (data) { self.Incr(data); });
})
.done((function () {
var request = $.ajax({
type: "POST",
dataType: 'json',
url: "UserSavedData",
data: { "InfoVal": $("#infoID").val() }
});
request.success(function (result) {
// here i use the result data to get ids of the dropdown values and
//fill the form
}
由于$getjson调用的网络定时,来自可观察数组的数据没有被填充(无法分配结果id对象,因为我想getjson-reponse需要更多的时间来加载)我该如何处理这个
根据建议,我使用了then()而不是done( 如果您希望您的promise按顺序链接,则需要使用 一旦第一个调用"完成",您现在拥有的代码就会立即运行,因为您并没有真正链接承诺,您只是在第一个承诺中添加了一堆成功处理程序。 下面是一个示例,显示了这两种方法之间的差异。 +1表示Josh。 一种更为Knockout的方法是将每个可观察的 等等。.then()
方法,并确保从成功处理程序返回任何新的promise,以便使它们可链接。$.getJSON('first')
.then(function(){
return $getJSON('second');
})
.then(function(){
return $.getJSON('third');
});
(function(){
function sayHelloAsync(msg){
var def = jQuery.Deferred();
setTimeout(function(){
$("<div>" + msg + "</div>").appendTo("#messages");
def.resolve();
}, getRandomInt(300, 1500));
return def.promise();
}
function clearMessages(){
$("#messages").html('');
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
function allAtOnce(){
clearMessages();
sayHelloAsync("first")
.done(function(){
sayHelloAsync("second");
})
.done(function(){
sayHelloAsync("third");
})
.done(function(){
sayHelloAsync("fourth");
});
}
function ordered(){
clearMessages();
sayHelloAsync("first")
.then(function(){
return sayHelloAsync("second");
})
.then(function(){
return sayHelloAsync("third");
})
.then(function(){
return sayHelloAsync("fourth");
});
}
$("#allAtOnce").on('click', allAtOnce);
$("#ordered").on('click', ordered);
}());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="allAtOnce" type="button">All At Once</button>
<button id="ordered" type="button">Ordered</button>
<h2>Messages</h2>
<div id="messages">
</div>
subscribe
设置为它所依赖的可观察对象(注意,您可以将observableArray作为成功函数传递,不需要包装它)。self.Irr = ko.observableArray([]);
self.Crp = ko.observableArray([]);
self.Incr = ko.observableArray([]);
self.Brnd = ko.observableArray([]);
self.Sec4 = ko.observableArray([]);
$.getJSON("GetIrr", null, self.Irr);
self.Irr.subscribe(function () {
$.getJSON("GetCrp", null, self.Crp);
});
self.Crp.subscribe(function () {
$.getJSON("GetTill", null, self.Sec4);
});
- 如何使用url加载程序在webpack中导入多个图像
- 如何在生成下载文件时显示加载动画
- 有没有任何方法可以将控制器从文件加载到ui路由器$stateProvider中
- 无法在通过jQuery的ajax加载的页面中执行javascript
- Emberjs应用程序加载在除Index之外的所有路由上
- 将“加载更多”添加到从Google电子表格中通过$.getJSON接收的数据输出中
- 使用 jQuery 的 ajax 或 getJSON 加载 .srj 文件
- $.ajax 和 $.getJson 加载的差异
- 使用 JQuery.getJSON() 从 JSON 的文件加载 contecxtmenu
- 在jQuery getJSON之后加载PaperScript
- EXT JS存储未从getJson调用正确加载数据
- 从$getJSON加载下拉值
- 使用$. getjson从JSON中加载地图数据
- JQuery Mobile页面在页面刷新之前不会加载getJSON内容
- 使用jQuery.getJSON加载JSON文件
- Javascript-使用$.getJson加载任意数量的URLS
- getJSON 加载速度慢,.toggle() 不触发
- 通过 getJSON 将 JSON 加载到变量中
- getJSON不能在IE中加载(任何版本)
- JQuery . getjson不会加载本地文件以及如何转换为JavaScript对象