jquery - 如何在SharePoint客户端对象模型中使用回调函数?

标签 jquery sharepoint sharepoint-2013

SharePoint 2013:仅当使用客户端对象模型 REST API 执行我的方法完成时,我才需要调用我的方法。请参阅下面的示例:

              <script type="text/javascript">
                function getListData() {
                    context = new SP.ClientContext.get_current();
                    web = context.get_web();
                    var list = web.get_lists().getByTitle('myList');
                    var myquery = new SP.CamlQuery();
                    myquery.set_viewXml("<View><Query><Where><IsNotNull><FieldRef Name='Title' />" +
                "</IsNotNull></Where></Query></View>");
                   myItems = list.getItems(myquery);
                    context.load(myItems);
                    context.executeQueryAsync(Function.createDelegate(this, function () { getListDataSuccess(); }), Function.createDelegate(this, this.getListDataFailed));
                }
                $(document).ready(function () {
                    getListData();
                    // I need to call my method here.
                    //Once getListDataSuccess method is completed then only I need to call some other function based on the result of getListDataSuccess method.
                });
            </script>

我可以在 getListDataSuccess 中调用我的方法,但这里的情况并非如此。还有其他几种方法相继使用客户端对象模型 REST API。这样就只剩下回调选项了。 JQuery 和 SPServices 中提供了类似的回调功能。但不确定它如何与 SharePoint 客户端对象模型配合使用?

最佳答案

由于 JSOM 是异步的,因此通常使用两种方法来控制 SharePoint 中异步调用的顺序执行:

  • 回调
  • 延期

回调方法

使用回调方法,您可以像这样声明函数

function getListData(listTitle,success,error)
{
    var context = SP.ClientContext.get_current();
    var web = context.get_web();
    var list = web.get_lists().getByTitle(listTitle);
    var items = list.getItems(SP.CamlQuery.createAllItemsQuery());

    context.load(items);
    context.executeQueryAsync(
        function() {
          success(items);           
        },
        error
    );
}

使用

getListData('Documents',
         function(items){
            console.log('Items count: ' + items.get_count());
         },
         function(sender,args){
            console.log('An error occured while retrieving list items:' + args.get_message());
});

延迟方法

延迟方法基于 Promises 模式,请参阅 this article有关使用 CSOM 的 Promise 的详细信息

A deferred - is a pattern that returns an object immediately from an asynchronous call

function getListData(listTitle,success,error)
{
    var dfd = $.Deferred(function () {
      var context = SP.ClientContext.get_current();
      var web = context.get_web();
      var list = web.get_lists().getByTitle(listTitle);
      var items = list.getItems(SP.CamlQuery.createAllItemsQuery());

      context.load(items);
      context.executeQueryAsync(
        function() {
           dfd.resolve(items);           
        },
        function (sender, args) {
           dfd.reject(args);
        }
      );
    });
    return dfd.promise();   
}

使用

getListData('Documents').then(function(items){
    console.log('Items count: ' + items.get_count());
});

关于jquery - 如何在SharePoint客户端对象模型中使用回调函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25185584/

相关文章:

sharepoint-2013 - Office 365 SharePoint 应用程序部署

azure - 将文件拖放到 Sharepoint 文件夹中时如何触发 Azure 逻辑应用

sharepoint - 在SharePoint中集成PowerShell

html - 文本转换 : lowercase is not working in email templates

javascript - 以编程方式更改输入字段中的值执行某些任务

javascript - 使用 JSOM 将错误记录到 ULS

rest - 从 SharePoint 下载文件

jquery - 如何查找 ID 与 json 键匹配的表 tr 并填充其他列

javascript - jQuery 验证 : Checked *or* Filled?

jQuery UI 在滚动 div 中可排序