jQuery Deferred 对象,按顺序调用函数

发布于 2025-01-07 19:26:33 字数 1302 浏览 0 评论 0原文

我对如何使用 jQuery 的 Deferred 对象感到困惑,并且我看到的示例对我没有帮助。我想要做的是 1.) 通过 ajax 调用获取日历对象,2.) 使用日历数据填充全局对象 (MYOBJ) 的一部分,然后 3.) 使用 MYOBJ 中的新数据填充页面元素。这三个函数实现了逻辑,我想按顺序调用它们:

function getCalendar(refDate, numDays) {
    return $.ajax({
        type: "POST",
        url: "services/Calendar.asmx/GetCalendar",
        data: '{ "refDate": "' + refDate + '", "numDays": "' + numDays + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    }).promise();
}


function loadCalendarData(response) {
    var calData = jQuery.parseJSON(response.d);
    MYOBJ.cal.dMin = calData.dMin;
    MYOBJ.cal.dMax = calData.dMax;
    MYOBJ.cal.dates = calData.dates; // array of date strings
}


function populateCalendar (x, y, z) {
    // use data from MYOBJ.cal here
}

不过,我不知道如何让 populateCalendar() 等待 loadCalendarData() 完成。这......

$.when(getCalendar(myDate, 70))
 .then(loadCalendarData)
 .then(populateCalendar(a, b, c))
 .fail(alertCalendarError);

显然是不正确的——这只是我扔到墙上的一种变体,因为我不明白我在做什么......:)

更新:作为GoldenNewby Brian ONeil 正确地指出,我可以在 loadCalendarData 末尾调用 populateCalendar 。那肯定会起作用。我希望我在发帖时就想到了这一点。我想我的最终目标是弄清楚如何实现测序。不过,在这种情况下,我无法想象在调用 loadCalendarData 后不直接调用 populateCalendar 的任何场景,所以这个解决方案绝对有意义。谢谢。

I'm confused about how to use jQuery's Deferred object, and the examples I've seen aren't helping me. What I want to do is 1.) get a calendar object via an ajax call, 2.) populate part of my global object (MYOBJ) with the calendar data, and then 3.) populate a page element with the new data in MYOBJ. These three functions implement the logic, and I want to call them in sequence:

function getCalendar(refDate, numDays) {
    return $.ajax({
        type: "POST",
        url: "services/Calendar.asmx/GetCalendar",
        data: '{ "refDate": "' + refDate + '", "numDays": "' + numDays + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    }).promise();
}


function loadCalendarData(response) {
    var calData = jQuery.parseJSON(response.d);
    MYOBJ.cal.dMin = calData.dMin;
    MYOBJ.cal.dMax = calData.dMax;
    MYOBJ.cal.dates = calData.dates; // array of date strings
}


function populateCalendar (x, y, z) {
    // use data from MYOBJ.cal here
}

I can't figure out how to make populateCalendar() wait until loadCalendarData() is done, though. This...

$.when(getCalendar(myDate, 70))
 .then(loadCalendarData)
 .then(populateCalendar(a, b, c))
 .fail(alertCalendarError);

...is obviously incorrect--it's just one variation I've thrown against the wall because I don't understand what I'm doing... :)

UPDATE: As GoldenNewby and Brian ONeil correctly point out, I could just stick my call to populateCalendar at the end of loadCalendarData. That will definitely work. I wish I had thought of that as I was posting. I guess my ultimate objective was to figure out how to achieve the sequencing. In this case, though, I can't think of any scenario where loadCalendarData would be called without a call to populateCalendar directly after it, so this solution definitely makes sense. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相思碎 2025-01-14 19:26:33

您实际上正在执行 populateCalendar(a, b, c)。您必须传递函数引用。试试这个。

$.when(getCalendar(myDate, 70))
 .then(loadCalendarData)
 .then(function(){
     populateCalendar(a, b, c)
  })
 .fail(alertCalendarError);

You are actually executing populateCalendar(a, b, c). You have to pass the function reference. Try this.

$.when(getCalendar(myDate, 70))
 .then(loadCalendarData)
 .then(function(){
     populateCalendar(a, b, c)
  })
 .fail(alertCalendarError);
只想待在家 2025-01-14 19:26:33

似乎唯一需要以延迟方式调用的方法是 loadCalendarData。

我会从您正在进行的 .ajax 调用的成功回调中调用 loadCalendarData,然后您可以在 loadCalendarData 末尾调用 populateCalendar (如评论中已经提到的)。

它看起来像这样......

function getCalendar(refDate, numDays) {
    return $.ajax({
        type: "POST",
        url: "services/Calendar.asmx/GetCalendar",
        data: '{ "refDate": "' + refDate + '", "numDays": "' + numDays + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: loadCalendarData
    });
}


function loadCalendarData(response) {
    var calData = jQuery.parseJSON(response.d);
    MYOBJ.cal.dMin = calData.dMin;
    MYOBJ.cal.dMax = calData.dMax;
    MYOBJ.cal.dates = calData.dates; // array of date strings

    populateCalendar(a, b, c); //not called until MYOBJ is setup
}


function populateCalendar (x, y, z) {
    // use data from MYOBJ.cal here
}

It seems like the only method that needs to be called in a deferred manner is loadCalendarData.

I would call loadCalendarData from the success callback of the .ajax call you are making and then you can just call populateCalendar at the end of loadCalendarData (as already mentioned in the comments).

it would look something like this...

function getCalendar(refDate, numDays) {
    return $.ajax({
        type: "POST",
        url: "services/Calendar.asmx/GetCalendar",
        data: '{ "refDate": "' + refDate + '", "numDays": "' + numDays + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: loadCalendarData
    });
}


function loadCalendarData(response) {
    var calData = jQuery.parseJSON(response.d);
    MYOBJ.cal.dMin = calData.dMin;
    MYOBJ.cal.dMax = calData.dMax;
    MYOBJ.cal.dates = calData.dates; // array of date strings

    populateCalendar(a, b, c); //not called until MYOBJ is setup
}


function populateCalendar (x, y, z) {
    // use data from MYOBJ.cal here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文