代码之家  ›  专栏  ›  技术社区  ›  gng

jQuery延迟/承诺与SPService库

  •  1
  • gng  · 技术社区  · 11 年前

    我正在尝试从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();
        }
    

    任何帮助都将不胜感激!

    3 回复  |  直到 11 年前
        1
  •  1
  •   getsetbro    10 年前

    我是这样做的:

    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
  •  0
  •   Roamer-1888    11 年前

    你确定吗 $().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;
        });
    }
    
        3
  •  0
  •   gng    11 年前

    通过进行以下更改,我现在可以让它工作了。我要把它贴出来,以防有人发现它有用。

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

    谢谢

    推荐文章