简体   繁体   English

jQuery完成动态拉取函数调用时

[英]jQuery When Done on dynamically pulled function call

I have the following code: 我有以下代码:

In site-code.js 在site-code.js中

.... ....

var ajaxContentFunc = $(origin).data("modal-content-handler");

$.when(window[ajaxContentFunc]()).done(function (resp) {
    kModal.showContent(resp);
});

In another file I have the following tag and function 在另一个文件中,我具有以下标记和功能

<a href="#" data-modal-content-handler="ajaxContentGeneration">Click Me</a>

.... ....

function ajaxContentGeneration() {

    var aProm = $.ajax({

        url: "tests/ajax/AjaxTest.aspx",
        data: { exampleType: "modal-ajax" },
        dataType: "html"


    });


    aProm.done(function (data) {

        console.log("Ajax Loaded!");
        var content = $(data).find("#ajax-content");

        return aProm;

    });


}

I need to populate the result of the ajaxContentGeneration (whatever method that might be) into the variable to send to showContent or in other words: 我需要将ajaxContentGeneration(可能的方法)的结果填充到变量中,以发送到showContent或换句话说:

1) Pull the ajaxContentFunction Name from the tag's modal-content-handler data attribute 1)从标记的modal-content-handler数据属性中提取ajaxContentFunction Name

2) Call function (in this case ajaxContentGeneration) 2)调用函数(在这种情况下为ajaxContentGeneration)

3) Wait for the function's ajax to complete and return the data generated (in this case html) 3)等待函数的ajax完成并返回生成的数据(在这种情况下为html)

4) When completed pass that value to kModal.showContent(----Here----); 4)完成后,将该值传递给kModal.showContent(---- Here ----);

However currently I am getting: 但是目前我得到:

1) Pulls ajaxContentFunctionName correctly 1)正确拉ajaxContentFunctionName

2) Calls Function (ajaxContentGeneration() function) 2)调用函数(ajaxContentGeneration()函数)

3) Calls kModal.showContent(undefined). 3)调用kModal.showContent(undefined)。 This is called prematurely because the deferred isn't correctly waiting for the function call to complete (after the ajax is done). 这被过早调用是因为deferred没有正确地等待函数调用完成(在ajax完成之后)。

4) Ajax Completes 4)Ajax完成

Where am I messing up here ? 我在哪里弄糟?

As far as I can tell, you are 95% there. 据我所知,您在那里95%。

Use .then() instead of .done() and return the promise returned by $.ajax().then() : 使用.then()而不是.done()并返回$.ajax().then()返回的promise:

function ajaxContentGeneration() {
    return $.ajax({
        url: "tests/ajax/AjaxTest.aspx",
        data: { exampleType: "modal-ajax" },
        dataType: "html"
    }).then(function (data) {
        return $(data).find("#ajax-content"); // this will return jQuery
        // return $(data).find("#ajax-content").html(); // this will return html
    });
}

You can probably also purge $.when() from the top-level call : 您可能还可以从顶级调用中清除$.when()

var ajaxContentFunc = $(origin).data("modal-content-handler");
window[ajaxContentFunc]().then(function (resp) {
    // `resp` is whatever was returned by the `return $(data).find()...` statement above
    kModal.showContent(resp);
});

The reason I say "probably" is that $.when() would be necessary if value-returning (not promise-returning) functions could be called instead of ajaxContentGeneration() . 我说的原因“可能”是$.when()是必要的,如果返回值的(不承诺返流)功能可以替代称为ajaxContentGeneration()

Another way would be to do: 另一种方法是:

// should really be renamed...
function ajaxContentGeneration(){
    return $.ajax({
        url      : "tests/ajax/AjaxTest.aspx",
        data     : { exampleType: "modal-ajax" },
        dataType : "html"
    })
}

Somewhere else: 别的地方:

var ajaxContentFunc = $(origin).data("modal-content-handler");

window[ajaxContentFunc]()
    .done(function(RES){
        kModal.showContent( $(RES).find("#ajax-content") );
    });

So the functionality of the ajaxContentGeneration function will be to return an AJAX promise, and not have it manipulated inside it, but do the manipulation where needed ( getting the #ajax-content element from the response ) 因此, ajaxContentGeneration函数的功能将是返回一个AJAX ajaxContentGeneration ,而不是在其内部进行操作,而是在需要的地方进行操作( 从响应中获取#ajax-content元素


Note that this whole thing is bad practice JS design, and you should avoid having functions on top of the window object, but instead on another object. 请注意,整个过程都是对JS设计的不好实践,您应该避免在window对象的顶部使用函数,而应在另一个对象上使用。

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

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