简体   繁体   English

jQuery链接的AJAX回调函数

[英]jQuery chaining of the AJAX callback functions

I have a centralized ajaxSuccess callback function that should initialize components returned by various AJAX calls around the project, something like this: 我有一个集中化的ajaxSuccess回调函数,该函数应初始化项目周围各种AJAX调用返回的组件,如下所示:

$(document).ajaxSuccess(function (response, status, xhr) {
    initComponents();
});

So the module-level ajax call could just be like: 因此,模块级ajax调用可能类似于:

$.ajax({
   url: "whatever",
   success: function(data) {
        // Do some stuff to update the DOM, like:
        $("container").html(data);
   }
});

without the need to perform the init of the eventual components I've in the returned HTML. 无需执行返回的HTML中包含的最终组件的初始化。

Now I've this issue: if the local ajax callback function is HUGE, I mean, if it performs a really heavy manulation of the DOM, it happens that the global ajaxSuccess function is fired BEFORE the elements returned by the call are placed in the DOM and available for jQuery. 现在,我遇到了一个问题:如果本地ajax回调函数是HUGE,我的意思是,如果它对DOM执行了非常繁重的操作,那么会发生全局ajaxSuccess函数在调用返回的元素放置在之前被触发。 DOM,可用于jQuery。

That's because the callback functions are not chained, just exectuted in sequence (I think). 这是因为回调函数没有链接,只是按顺序执行(我认为)。

There is a way to be sure to perform the global ajaxSuccess after the local success callback function is completed? 有一种方法可以确保在本地成功回调函数完成之后执行全局ajaxSuccess吗?

Or, there is a better way to manage these behaviours? 或者,有更好的方法来管理这些行为?

UPDATE: Doing some logging I'm pretty sure the HTML is in place BEFORE we even entry on the global ajaxSuccess. 更新:做一些日志记录我很确定在我们甚至在全局ajaxSuccess上输入之前,HTML到位。 The issue still remains, anyway, as specific jQuery selectors used in the initComponents() function is not retrieving the elements injected in the DOM. 无论如何,问题仍然存在,因为initComponents()函数中使用的特定jQuery选择器没有检索注入DOM中的元素。

If I perfom the initComponents() inside a setTimeout callback, it is working.... but I don't want to use a timeout... 如果我在setTimeout回调中执行initComponents(),则它正在工作....但是我不想使用超时...

It's like the updated DOM is not yet queryable when ajaxSuccess fires. 就像ajaxSuccess触发时更新的DOM尚不可查询。

UPDATE 2: False alarm. 更新2:错误警报。 Actually the update of the html was inside a fadeOut callback function that was making it async in relation of other functions... My fault. 其实html的更新是在fadeOut回调函数内部,该函数使它与其他函数保持异步...我的错。

尝试将global:true属性添加到$ .ajax对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM