【问题标题】:jQuery deferred/promise with SPService libraryjQuery 延迟/承诺与 SPService 库
【发布时间】:2015-02-27 16:44:58
【问题描述】:

我正在尝试从 SPService SPGetListItemsJson 获取我的承诺项目。问题是,当 SPGetListItemsJson 被调用并且 requestPromise 完成并且延迟被解决时,我希望数据能够传递到我在 populateDropDownControlWithList 中的匿名函数,但它是未定义的。

   function populateDropDownControlWithList(option, control, addSelectValue)
    {
        if (typeof (addSelectValue) === 'undefined')
        {
            addSelectValue = false;
        }

        var selectedIndex = control.val() ? control.val() : -1;    
        if (addSelectValue)
        {
            control.append('<option value=-1>Select an Option</option>');
        }

        var request = SPGetListItemsJson(option);
        request.done(function (data) // Expect the json object here but it is undefined
        {
            $.each(data, function () 
            {
                controlappend('<option value=' + this.Id + '>' + this.Title + '</option>');
            });
        });
    }

    function SPGetListItemsJson(option)
    {
        var deferred = $.Deferred();
        var requestsPromise = $().SPServices.SPGetListItemsJson({
            listName: option.Name,
            CAMLQuery: option.Query,
            CAMLViewFields: option.View,
            mappingOverrides: option.Mapping,
            debug: option.Debug,
            async: option.async
        });

        requestsPromise.done(function ()
        {
            deferred.resolveWith(this.data); // Verified this.data is populated with the correct result
        });

        return deferred.promise();
    }

任何帮助将不胜感激!

【问题讨论】:

    标签: jquery jquery-deferred spservices


    【解决方案1】:

    这是我的做法:

    function getListOne() {
        var dfd = $.Deferred();
        $().SPServices({
            operation: "GetListItems",
            listName: "ListOne",
            completefunc: function(data, status) {
                console.log("first is done");
                if (status == "success") {
                    //do stuff
                    dfd.resolve("");
                } else {
                    dfd.reject();
                }
            }
        });
        return dfd.promise();
    }
    
    function getListTwo() {
        var dfd = $.Deferred();
        $().SPServices({
            operation: "GetListItems",
            listName: "ListTwo",
            completefunc: function(data, status) {
                console.log("second is done");
                if (status == "success") {
                    //do stuff
                    dfd.resolve("");
                } else {
                    dfd.reject();
                }
            }
        });
        return dfd.promise();
    }
    $.when(getListOne(), getListTwo()).then(function() {
        console.log("both are done");
    });
    

    更容易证明它正在使用 2 个列表和控制台日志。

    【讨论】:

      【解决方案2】:

      你确定$().SPServices.SPGetListItemsJson() 会返回一个承诺吗?

      SPServices 文档含糊不清。它说该方法返回一个 JavaScript(普通)对象,然后继续给出一个例子,说 var traineePromise = $().SPServices.SPGetListItemsJson({...}) 并继续 $.when(traineePromise).done(...)

      因此,您最好将SPGetListItemsJson() 写成如下:

      function SPGetListItemsJson(option) {
          var obj = $().SPServices.SPGetListItemsJson({
              listName: option.Name,
              CAMLQuery: option.Query,
              CAMLViewFields: option.View,
              mappingOverrides: option.Mapping,
              debug: option.Debug,
              async: option.async
          });
          return $.when(obj).then(function () {
              return this.data;
          });
      }
      

      【讨论】:

      • 是 SPGetListItemsJson() 返回一个承诺。
      【解决方案3】:

      通过进行以下更改,我现在可以让它工作。我发布它以防有人觉得这很有用。

      request.done(function ()
      {
          $.each(this, function () 
          {
              control.append('<option value=' + this.ID + '>' + this.Title + '</option>');
          });
      });
      

      谢谢!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-27
        • 2023-03-21
        • 1970-01-01
        • 2011-07-23
        • 2012-09-22
        • 2015-08-25
        相关资源
        最近更新 更多