【问题标题】:jquery deferredjQuery延迟
【发布时间】:2011-03-02 01:26:55
【问题描述】:

我在学习使用新的 jQuery Deferred 时遇到了困难。

执行 ajax 调用,我想返回 ajax 调用的数据。

checkIDExists = function(id){
    var exists = false;
    $.ajax({
        data: {
            method: "idExists",
            id: id
        },
        success: function(data){
                if(data == 'true'){
                    exists = true;
                }
        }
    }).done(function(){
        return exists;
    }).fail(function(){
        return false;
    });
};

我知道当我尝试在 done() 或 fail() 函数中返回某些内容时,问题就出现了,而 checkIdExists() 函数没有返回它。我该如何解决这个问题?

【问题讨论】:

    标签: jquery jquery-deferred


    【解决方案1】:

    据我所知,Lloyd 的陈述是正确的,但我不认为这正是您正在寻找的答案,这是我的尝试:

    首先,在使用延迟承诺时,唯一合理的期望和作为返回值提供的是承诺对象(因此 Lloyd 将您指向 CPS)。

    你会在哪里通常做类似的事情

    /* Have some kind of callback for when ajax is done */
    
    var myCompleteCallback = function(data){
       // whatever you want to do with your ajax call results
    }
    
    var myErrorCallback = function(){
       // handle the ajax error
    }
    
    /* Send the actual ajax request, and tell it to call MyCompleteCallback afterwards */
    
      $.ajax({
        url: '/foo/bar.xml'
        data: {},
        success: myCompleteCallback,
        error: 
      });
    

    你会在延迟风格的实现中这样做:

    /* Have some kind of callback for when promise is resolved is done */
    
    var myCompleteCallback = function(data){
       // whatever you want to do with your ajax call results
    }
    
    var myErrorCallback = function(){
       // handle the ajax error
    }
    
    /* you could also do ajax.done().fail() but i think this reads better as an example */
    
    var getsomething = $.ajax({ url: '/foo/bar.xml', data: {} });
    getsomething.then( myCompleteCallback, myErrorCallback ) 
    

    如您所见,除了您开始研究更复杂的示例外,它并没有什么神奇和不同之处。

    不过它有什么酷的(根据前面的示例)...

    var getVisitorInfo = function(){
    
      /* stash the user information ajax call promise */
    
      var fetchUserInfo    = $.ajax({url:"/some/api/user.json"})
    
      /* stash the account information ajax call promise */
    
      var fetchAccountInfo = $.ajax({url:"/some/api/user.json"})
    
      /* trigger both calls and returns a promise that will resolve to both results */
    
      return $.when( fetchUserInfo,  fetchAccountInfo )
    }
    
    /* Usage: */
    
    getVisitorInfo().done(function(userJSON, accountJSON){
       // manipulate your data/ui/and whatnot
    }).fail(function(failure1,failure2){
       // redirect to login or whatever
    })
    

    希望这会有所帮助。我建议看一下各种延迟/承诺的实现,以更好地理解这一切。真正帮助我的是使用Kris Kowal's Q 库(以及他提供的优质自述文件)并在CommonJS wiki 上阅读它。而且Kris也给了talk on the topic back in 2010

    【讨论】:

      【解决方案2】:

      Ajax 本身是异步工作的,因此函数 checkIDExists 会在 ajax 调用从服务器返回数据之前完成。

      在您的情况下,我不会依赖 checkIDExists 函数的返回值,但我会使用 CPS 方法覆盖该函数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-20
        • 2012-05-10
        • 2011-09-15
        • 2011-09-11
        • 2012-07-08
        • 2021-12-02
        相关资源
        最近更新 更多