简体   繁体   English

jQuery多延迟链接

[英]jquery multiple deferred chaining

This is not working at all for me. 这对我来说根本不起作用。 Basically, if I step through it, it performs in the order where I have breakpoints (BREAKPOINT1, BREAKPOINT2, etc) 基本上,如果我逐步执行它,它将按照我有断点的顺序执行(BREAKPOINT1,BREAKPOINT2等)

Due the way I am required to do sharepoint development at the moment, everything must be asynchronous. 由于目前需要进行共享点开发的方式,所有内容都必须是异步的。 Loading data for one edit list item results in maybe 10-12 asynchronous calls. 加载一个编辑列表项的数据可能会导致10-12次异步调用。 So I have chains several layers deep everywhere. 所以我到处都有几层深的链。 This is the one that is completely breaking the functionality. 这是完全破坏功能的工具。

EDIT- What I want to happen. 编辑-我想发生的事情。 I want to call get children, get the group id's from a list, call getgroupname and get the group names for all id's (as many as 50) and then dump the array contents into a hidden field so when I update the list, it doesn't go and create a new record for each of these group ids as it would on a new list item. 我想调用get children,从列表中获取组ID,调用getgroupname并获取所有ID(多达50个)的组名,然后将数组内容转储到隐藏字段中,因此当我更新列表时,它不会不要像在新列表项上那样为每个组ID创建新记录。 So when I click "update" it will grab the id's and create a new listitem for any group not present in the hidden field. 因此,当我单击“更新”时,它将获取ID并为隐藏字段中不存在的任何组创建一个新的列表项。 This is a parent/child relationship, each group that is selected results in a new list item, but I only need one per group, not multiple. 这是一个父子关系,每个选定的组都会产生一个新的列表项,但是每个组只需要一个,而不是多个。

I need to know how to fix this chain so that it returns my group names to the hidden field. 我需要知道如何修复此链,以便它将我的组名返回到隐藏字段。 I'm just having issues getting there and am hoping someone can spot my breakdown in logic. 我只是遇到问题,希望有人能发现我的逻辑故障。

 function getchildren(id) { var retval = []; var url = "call to sharepoint 2013 url"; var ajaxPromise = restfulAPICall(url); //inside of this includes return $.ajax ajaxPromise.then(function (data) { $.each(data.d.results, function (i, v) { var def = new $.Deferred(); getGroupname(v.Assigned_x0020_ToId.results[0]).done( //BREAKPOINT4 - multiple hits function (val) { def.resolve(val); //BREAKPOINT5 } ) .done(function () { reval.push(def); //BREAKPOINT6 (then back to 5, etc for # of results) }); }); }); return $.when.apply(undefined, retval).promise(); //BREAKPOINT2 } function getGroupName(v) { var d = $.Deferred(); var ctx = new SP.ClientContext.get_current(); var groups = ctx.get_web().get_siteGroups(); var groupname = groups.getById(v); ctx.load(groupname); ctx.executeQueryAsync(function () { d.resolve(groupname.get_title()); }, function (sender, err) { d.reject(err); }); return d.promise(); } //somewhere else if (!isChild) { getChildren(id).then(function (val) { //BREAKPOINT1 $('#children').val(val); //BREAKPOINT3 - value blank }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="hidden" id="children" /> 

I suppose the heart of the matter is that, in getChildren , return $.when.apply(undefined, retval).promise(); 我想问题的核心在于,在getChildrenreturn $.when.apply(undefined, retval).promise(); executes immediately, before any of your async code has had time to do anything, and so retval remains an empty array and the returned promise immediately resolves to an empty array. 在您的任何异步代码都没有时间做任何事情之前,立即执行,因此retval仍然是一个空数组,并且返回的promise立即解析为一个空数组。 The fix might look something like this, or at least this should point you in the right direction: 该修复程序可能看起来像这样,或者至少这应该为您指明正确的方向:

function getChildren() {
  var deferredResult = $.Deferred();
  var url = "call to sharepoint 2013 url";
  var ajaxPromise = restfulAPICall(url);
  ajaxPromise.then(function (data) {
    var retval = [];
    $.each(data.d.results, function (i, v) {
      retval.push(getGroupName(v.Assigned_x0020_ToId.results[0]));
    });
    // Only resolve promise returned from getChildren() once you have actually processed all the group names. Array.prototype.slice.call(arguments) converts arguments to real array
    $.when.apply(undefined, retval).then(function() {
      deferredResult.resolve(Array.prototype.slice.call(arguments));
    });
  });

  return deferredResult.promise();
}

Here is a fiddle , simplified from your originally provided code, with some mocked AJAX data to see if this might be what you are looking for... 这是从原始提供的代码简化而来的小提琴 ,带有一些模拟的AJAX数据,以查看这是否是您要寻找的...

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

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