Javascript 返回.executeQueryAsync中的变量

Javascript 返回.executeQueryAsync中的变量,javascript,jquery,sharepoint,Javascript,Jquery,Sharepoint,我有这个代码,它提醒我想要的信息,但我不知道如何修改它以显示我的选择列表,我只需要知道如何更改此代码并替换提醒(listItemInfo)按返回列表项信息显示我的自定义列表而不是SharePoint的默认列表 var lookupSample = lookupSample || {}; var siteUrl = '/sites/studentday/bat/testJS'; lookupSample.CustomizeFieldRendering = function() { // In

我有这个代码,它提醒我想要的信息,但我不知道如何修改它以显示我的选择列表,我只需要知道如何更改此代码并替换
提醒(listItemInfo)
返回列表项信息
显示我的自定义列表而不是SharePoint的默认列表

var lookupSample = lookupSample || {};
var siteUrl = '/sites/studentday/bat/testJS';

lookupSample.CustomizeFieldRendering = function() {
  // Intialize the variables for overrides objects
  var overrideCtx = {
    Templates: {
      Fields: {
        'Supplier': {
          'NewForm': ExecuteOrDelayUntilScriptLoaded(lookupSample.singleLookupValue, "sp.js")
        }
      }
    }
  };
  // Register the override of the field
  SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
}

lookupSample.singleLookupValue = function(ctx) {
  var clientContext = new SP.ClientContext(siteUrl);
  var oList = clientContext.get_web().get_lists().getByTitle('Suppliers');
  var camlQuery = new SP.CamlQuery();
  camlQuery.set_viewXml('<View><Query><GroupBy><FieldRef Name = "Category"/></GroupBy><OrderBy><FieldRef Name = "Title"/></OrderBy></Query></View>');
  this.collListItem = oList.getItems(camlQuery);
  clientContext.load(collListItem);
  clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded(sender, args) {
  var listItemInfo = [];
  var listItemEnumerator = collListItem.getEnumerator();
  var tempo = '';
  listItemInfo.push('<select id="DdList" style="width: 200px;">');
  while (listItemEnumerator.moveNext()) {
    var oListItem = listItemEnumerator.get_current();
    if (tempo == oListItem.get_item('Category')) {
      listItemInfo.push('\n    <option value ="' + oListItem.get_id() + '">' + oListItem.get_item('Title') + '</option>');
    } else {
      if (tempo != "") {
        listItemInfo.push('\n  </optgroup>');
      }
      listItemInfo.push('\n  <optgroup label ="' + oListItem.get_item('Category') + '">');
      listItemInfo.push('\n    <option value ="' + oListItem.get_id() + '">' + oListItem.get_item('Title') + '</option>');
      tempo = oListItem.get_item('Category');
    }
  }
  listItemInfo.push('\n  </optgroup>');
  listItemInfo.push('\n</select>');
  alert(listItemInfo);
}

function onQueryFailed(sender, args) {
  listItemInfo.push('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

lookupSample.CustomizeFieldRendering();
var lookupSample=lookupSample | |{};
var siteUrl='/sites/studentday/bat/testJS';
lookupSample.CustomizeFieldRendering=函数(){
//初始化覆盖对象的变量
var overrideCtx={
模板:{
字段:{
“供应商”:{
'NewForm':ExecuteOrderLayUntilScriptLoaded(lookupSample.singleLookupValue,“sp.js”)
}
}
}
};
//注册该字段的重写
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(OverrideCX);
}
lookupSample.singleLookupValue=函数(ctx){
var clientContext=new SP.clientContext(siteUrl);
var oList=clientContext.get_web().get_list().getByTitle('Suppliers');
var camlQuery=new SP.camlQuery();
camlQuery.set_viewXml(“”);
this.collListItem=oList.getItems(camlQuery);
加载(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceed),Function.createDelegate(this,this.onQueryFailed));
}
函数onquerysucceed(发送方,参数){
var listItemInfo=[];
var listItemEnumerator=collListItem.getEnumerator();
var tempo=“”;
listItemInfo.push(“”);
while(listItemEnumerator.moveNext()){
var oListItem=listItemEnumerator.get_current();
if(tempo==oListItem.get_项('Category')){
listItemInfo.push('\n'+oListItem.get_item('Title')+'');
}否则{
如果(节奏!=“”){
listItemInfo.push('\n');
}
listItemInfo.push('\n');
listItemInfo.push('\n'+oListItem.get_item('Title')+'');
tempo=oListItem.get_项目(“类别”);
}
}
listItemInfo.push('\n');
listItemInfo.push('\n');
警报(listItemInfo);
}
函数onQueryFailed(发送方,参数){
listItemInfo.push('请求失败。'+args.get_message()+'\n'+args.get_stackTrace());
}
lookupSample.CustomizeFieldRendering();

不能直接从异步函数返回值

不要在异步函数的末尾返回一个值,而是编写一个新函数来执行您要使用该返回值执行的任何操作,并调用该函数

例如,与此相反:

var asyncResult = asyncDoSomething();
doSomethingWithResult(asyncResult); // WRONG

function asyncDoSomething(){
    var something;
    // logic here
    return something;
}
…遵循以下模式:

asyncDoSomething();
// no more code should be executed in this context after calling async function

function asyncDoSomething(){
    var something;
    // logic here
    doSomethingWithResult(something); // RIGHT: logic moved forward
}
在调用异步函数之后,不要将其他代码行放在同一范围内

相反,将该逻辑向前传递,这可能意味着将其嵌入到异步函数中,或者(通常)将回调函数传递给异步函数,以便在异步函数完成任何操作时调用

现代库通常通过让异步函数接受两个参数来解决此模式,这两个参数指示在完成时调用哪些函数<例如,code>executeQueryAsync
有两个参数:成功时执行的函数和失败时执行的函数


JavaScript“承诺”的工作原理类似,每个承诺对象都允许您指定在承诺返回时调用的函数(通过
then()
函数定义)。

谢谢您的回答!事实上,我需要检索listItemInfo变量并将其替换为
ExecuteOrderLayuntilScriptLoaded(lookupSample.singleLookupValue,“sp.js”)
,因为我需要在新表单中的特定位置显示此选择列表。我可以使用承诺,但我不知道它是如何工作的,我试图为我的代码应用承诺,但它不起作用。