我需要设置异步回调,因为函数从远程位置获取内容。我正在这样做:
$.when( priv[box.view.renderWith](content, box.view.gadget_id) ).then(function(late) {
console.log("done");
console.log(late)
console.log($(content))
$(content).append(late).enhanceWithin();
});
使用我的 when
函数触发单个 Ajax 请求。在它的回调中,我返回一个元素以附加到 $(content)
。
我的问题是,then
函数在我的 ajax 回调运行并返回某些内容之前就立即触发。
问题:
是否无法将 when()
与发出 ajax 请求的函数一起使用?我是否必须直接在 when()
中发出 ajax 请求?或者为什么then()
会立即被触发?我该如何解决这个问题?
谢谢!
编辑: 我当前版本的代码片段:
$.when( priv[box.view.renderWith](content, box.view.gadget_id) ).then(function(fragment) {
// DOM manip...
console.log("NOW WE ARE DONE WITH WHEN");
console.log(fragment)
$(content).append(fragment).enhanceWithin();
});
我正在调用的函数(没有内容生成部分):
priv.constructListbox = function (element, internal) {
var no_data_body,
no_data_cell,
portable,
gadget_id = element.getAttribute("data-gadget-id") || internal,
settings = priv.gadget_properties[gadget_id],
portal_type = settings.portal_type_title,
// wrapper
$parent = $(element.parentNode);
if (settings !== undefined) {
// ASYNC > this will trigger an Ajax request
portable = priv.erp5.allDocs({
"query": "type: \"" + settings.datasource + "\"",
"limit": [0, (settings.configuration.pagination.items_per_page_select[0] || 30)],
"wildcard_character": "%",
"include_docs": true
}).always(function (answer) {
.... stuff ...
// finish
// return to calling function
if (internal) {
console.log("foo");
console.log("no we only give back a fragment");
return fragment_container;
}
$parent.empty().append( fragment_container ).enhanceWithin();
});
// if internal call, return the promise object
if (internal) {
console.log("foo internal, promise");
return portable;
}
} else {
// error handler
}
};
当我在 then
回调中控制台 portable
时,我得到了 promise
对象,所以现在该函数返回的是 Promise 与一个元素。然而,当解决后,我希望当我没有......得到任何东西时我能得到我的 fragment_container
:-(
希望足够清楚。
最佳答案
我听过的最好建议是将异步编程视为普通函数,然后在末尾添加 promise 。
我很难看到您在哪里设置fragment_container,但这里是..
priv.constructListbox = function (element, internal) {
var dfd = new $.Deferred();
...
if (settings !== undefined) {
portable = priv.erp5.allDocs({
"query": "type: \"" + settings.datasource + "\"",
"limit": [0, (settings.configuration.pagination.items_per_page_select[0] || 30)],
"wildcard_character": "%",
"include_docs": true
}).always(function (answer) {
.... stuff ...
// finish
// return to calling function
if (internal) {
console.log("foo");
console.log("no we only give back a fragment");
dfd.resolve({message:"You did it!", element: fragment_container });
}
$parent.empty().append( fragment_container ).enhanceWithin();
});
} else {
dfd.reject({result:"Nope - no data came out"});
// error handler
}
return dfd.promise();
};
然后就可以轻松查看您返回的内容:
$.when( priv[box.view.renderWith](content, box.view.gadget_id) ).then(function(fragment) {
console.log("NOW WE ARE DONE WITH WHEN");
console.log(fragment);
},
function(fragment) {
console.log("It failed");
console.log(fragment);
});
关于javascript - 为什么我的 jQuery when().then() 函数会在 ajax 请求完成之前触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18764625/