jQuery的承诺,并与DEFERED返回的结果 [英] Jquery Promise and Defered with returned results

查看:249
本文介绍了jQuery的承诺,并与DEFERED返回的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Backbone.js的,我有一些事件,产生了选项对象设置,发生在我的路由器。被调用视图需要这些对象,因此,在创建视图之前,他们必须完成。问题是发生这些事件Ajax和是异步的,所以显示的视图之前不完成。我试图使事件同步的,但导致其他的发行,冻结一样的GUI。所以,我想我的连锁功能,使该视图,直到在所有的功能都被称为创建。但是,这不是为我工作,因为我似乎无法弄清楚如何延迟执行调用之间传递数据。以下是我有:

Router.js:

  someParentFunction:功能(paramA上,paramB){
     VAR认为这=;
     VAR defer1 = $。当(
        $获得(that.functionA('somedata1','somedata2,即))
     );
      defer1.done(函数(){
          VAR defer2 = $。当(
              $获得(that.functionB('someData,即))
          );
          defer2.done(功能(数据){
              VAR defer3 = $。当(
                 $获得(that.functionC('somedata1,即))
               );
               defer3.done(功能(数据){
               //我怎么从每个递延函数的结果?
               //基林记住每个延迟功能
               //还接收参数。
               //同时,其他功能的顺序并不重要,
               //只要他们都在这之前返回它们的值
               //创建视图。
               that.view =新ProjectView({
                  someParam1:paramA上,
                  someParam2:paramB,
                  resultsA:jQuery.parseJSON(defer1.results)
                  resultsB:jQuery.parseJSON(defer2.results)
                  resultsC:jQuery.parseJSON(defer3.results)                 }),                 window.app.page(that.view,{
                       标签:'someName',
                 })               });
          });      });
}泛函:函数(参数1,参数2){
  VAR URL ='Q = somestring&放大器;? +参数1 +'和;' + param2的;
  返回$阿贾克斯({
            网址:网址,
            背景:背景下,
            beforeSend:功能(XHR){
            );
            }
            })。成功(功能(数据){
            })responseText的;
    },
functionB:函数(参数1,上下文){
  VAR URL ='Q = somestring&放大器;? +参数1;
  返回$阿贾克斯({
            网址:网址,
            背景:背景下,
            beforeSend:功能(XHR){
            );
            }
            })。成功(功能(数据){
            })responseText的;
    },
functionC:函数(参数1,上下文){
  VAR URL ='Q = somestring&放大器;? +参数1;
  返回$阿贾克斯({
            网址:网址,
            背景:背景下,
            beforeSend:功能(XHR){
            );
            }
            })。成功(功能(数据){
            })responseText的;
    },


解决方案

在这一段时间工作后,这是我找到的工作:

这本=

  VAR
       $。当(
            that.functionA(参数1,即)
            that.functionB(参数1,即)
            that.functionC(即)
            that.functionD(参数1,即)
            that.functionE(参数1,即)
            that.functionF(参数1,也就是说,参数2)
        ).done(功能(A1,A2,A3,A4,A5,A6){
            变种response1 = jQuery.parseJSON(A1 [0] .result.results);
            变种响应2 = jQuery.parseJSON(A2 [0] .result.results);
            变种response3 = jQuery.parseJSON(A3 [0] .result.results);
            变种response4 = jQuery.parseJSON(A4 [0] .result.results);
            变种response5 = jQuery.parseJSON(A5 [0] .result.results);
            变种response6 = jQuery.parseJSON(A6 [0] .result.results);            that.view =新的MyView的({
                someParam:参数1,
                anotherParam:参数2,
                R1:response1,
                R2:响应2,
                R3:response3,
                R4:response4,
                R5:response5,
                R6:response6
            }),            window.app.page(that.view,{
                标签:'someValue中',
            })
    });

然后每个函数的结构是这样的:

 泛函:函数(参数1,上下文){
        VAR URL ='Q = MyApp来/ API / general_functions /&放大器;参数1 ='+参数1;
        返回$阿贾克斯({
            网址:网址,
            背景:背景下,
            beforeSend:功能(XHR){
            }
        })。成功(功能(数据){
        });
    },

这确保了各项功能,包裹在。当法,渐至中,.done方法之前完成。

希望这可以帮助别人。

I am using Backbone.js and I have a number of events, that produce settings for the Options object, that occur in my router. The view that gets called needs these objects and so, they must complete before the view is created. The problem is that these events that occur are ajax and are asynchronous, so they don't complete before the view is displayed. I tried making the events synchronous, but that causes other issued, like freezing the gui. So, I'm trying to chain my functions so that the view is created until after all the functions have been called. But, this is not working for me, as I can't seem to figure out how to pass data between defered calls. Here is what I have:

Router.js:

someParentFunction:function(paramA, paramB){
     var that = this;
     var defer1 = $.when(
        $.get(that.functionA('somedata1','somedata2',that))
     );
      defer1.done(function () {
          var defer2 = $.when(
              $.get(that.functionB('someData',that))
          );
          defer2.done(function (data) {
              var defer3 = $.when(
                 $.get(that.functionC('somedata1',that))
               );
               defer3.done(function (data) {
               //how do I get the results from each Deferred function?
               //keeling in mind that each deferred function 
               //also receives parameters.
               //also, the order of the other functions does not matter,
               //as long as they all return their values before this 
               //view is created.
               that.view = new ProjectView({
                  someParam1:paramA,
                  someParam2:paramB,                      
                  resultsA: jQuery.parseJSON(defer1.results),
                  resultsB: jQuery.parseJSON(defer2.results),
                  resultsC: jQuery.parseJSON(defer3.results),

                 }),

                 window.app.page(that.view, {
                       tab:'someName',                          
                 })

               });
          });

      });
}

functionA: function(param1, param2){
  var url = '?q=somestring&' + param1 + '&' + param2 ;
  return  $.ajax({
            url: url,
            context: context,
            beforeSend: function( xhr ) {
            );
            }
            }).success(function( data ) {               
            }).responseText;
    },
functionB: function(param1, context){
  var url = '?q=somestring&' + param1  ;
  return  $.ajax({
            url: url,
            context: context,
            beforeSend: function( xhr ) {
            );
            }
            }).success(function( data ) {               
            }).responseText;
    },
functionC: function(param1, context){
  var url = '?q=somestring&' + param1;
  return  $.ajax({
            url: url,
            context: context,
            beforeSend: function( xhr ) {
            );
            }
            }).success(function( data ) {               
            }).responseText;
    },

解决方案

After working on this for awhile, this is what I found to work:

       var that = this
       $.when(
            that.functionA(param1,that) ,
            that.functionB(param1,that)  ,
            that.functionC(that)  ,
            that.functionD(param1,that)  ,
            that.functionE(param1,that) ,
            that.functionF(param1,that, param2)
        ).done(function( a1, a2 , a3, a4, a5, a6) {
            var response1 = jQuery.parseJSON(a1[0].result.results);
            var response2 = jQuery.parseJSON(a2[0].result.results);
            var response3 = jQuery.parseJSON(a3[0].result.results);
            var response4 = jQuery.parseJSON(a4[0].result.results);
            var response5 = jQuery.parseJSON(a5[0].result.results);
            var response6 = jQuery.parseJSON(a6[0].result.results);

            that.view = new MyView({
                someParam:param1,
                anotherParam:param2,                    
                r1: response1,
                r2: response2,
                r3: response3,
                r4: response4,
                r5: response5,
                r6: response6
            }),

            window.app.page(that.view, {
                tab:'someValue',           
            })
    });

Then each function was structured like this:

functionA: function(param1, context){
        var url = '?q=myApp/api/general_functions/&param1=' + param1;
        return  $.ajax({
            url: url,
            context: context,
            beforeSend: function( xhr ) {
            }
        }).success(function( data ) {
        });
    },

This ensured that each function, wrapped in the .when method, completed before getting to the .done method.

hope this helps anyone else.

这篇关于jQuery的承诺,并与DEFERED返回的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆