将JSOM代码转换为同步 [英] Convert JSOM code to synchronous

查看:59
本文介绍了将JSOM代码转换为同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有JSOM脚本以从不同DIV中不同国家/地区的页面上的SharePoint列表中获取数据,我正在document中调用该函数.

have the JSOM script to get data from a SharePoint list on my page of different countries in different DIV, i am calling the function in document.ready as many number of times as there are number of countries,

但是我在调​​用该函数之前有一个警报,如果我删除了alert(country_list [i])代码不起作用.

but i have an alert just before i call the function and if i remove the alert(country_list[i]) code does not work.

我该如何解决?

<script type="text/javascript">
 $(document).ready(function(){
    var country_list = ["Belgium", "Brazil","Canada", "Germany","USA", "Other countries"];
    for(var i=0; i<country_list.length; i++)
    {
//if i remove this alert the code does not work
        alert(country_list[i]);
        retrieveListItems_berba(country_list[i]);
    }
    $(".accordion_head").click(function(){  
        //retrieveListItemsBelgium();

        if ($('.accordion_body').is(':visible')) {
            $(".accordion_body").slideUp(600);
            $(".plusminus").text('+');
        }
        $(this).next(".accordion_body").slideDown(600); 
        $(this).children(".plusminus").text('-');
    //alert("hello");
    });


});

function retrieveListItems_berba(country) {
    var clientContext = new SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('xyz');

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name=\'Country\' /><Value Type=\'Choice\'>"+ country +"</Value></Eq></Where></Query>" + 
        '<RowLimit>100</RowLimit></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 counter = 1;
    var LiTextCountry = '';
    var listItemInfo = '';
    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {

        var oListItem = listItemEnumerator.get_current();
        //alert("a"+counter);
        var ContactName= oListItem.get_item('ContactName')
        //alert("b"+counter);
        var Title= oListItem.get_item('Title'); 
        var Country= oListItem.get_item('Country');
        var image= oListItem.get_item('Image').get_url(); 

        var ULIDCountry= document.getElementById(Country+counter);

        LiTextCountry = "<div class='media-left'><img src='" + image + "' class='media-object' style='width:60px'></div>" +
                  "<div class='media-body'><h4 class='media-heading'>" + ContactName +"</h4><span>" + Title + "</span>"+
                  "</div>";

        //alert(String.raw`${LiTextCountry}`);
         ULIDCountry.innerHTML = LiTextCountry;
         counter++;

    }
}

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


</script>


谢谢

帕鲁

Paru

推荐答案

paru,

建议您使用Ajax调用REST API通过设置参数["async":false]同步检索列表项.

您可以参考:

You can refer to: https://sharepoint.stackexchange.com/questions/208020/make-caml-query-with-in-rest-api-call

您的代码如下所示:

var viewXml = {
            ViewXml: "<View><Query><Where><Eq><FieldRef Name=\'Country\' /><Value Type=\'Choice\'>" + country + "</Value></Eq></Where></Query>" +
                     "<RowLimit>100</RowLimit></View>"
        }

        var call =


.ajax({url:_spPageContextInfo.webAbsoluteUrl +``/_api/Web/Lists/getByTitle('xyz')/GetItems(query = @ v1)?'' +"@ v1 =" + JSON.stringify(viewXml), 类型:"POST", dataType:"json", 标头:{ 接受:"application/json; odata = verbose", "X-RequestDigest":
.ajax({url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getByTitle('xyz')/GetItems(query=@v1)?" +"@v1=" + JSON.stringify(viewXml), type: "POST", dataType: "json", headers: { Accept: "application/json;odata=verbose", "X-RequestDigest":


("#__ REQUESTDIGEST").val() }, 异步:假 });
("#__REQUESTDIGEST").val() }, async: false });


有关REST API的更多信息,供您参考. /span>


More information about REST API for your reference.

如果您想使用JSOM来解决问题,您可以参考下面的文章.

最好的问候,

Lee Liu


这篇关于将JSOM代码转换为同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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