1

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();
    }

どんな助けでも大歓迎です!

4

3 に答える 3

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 つのリストとコンソール ログで動作していることを証明するのは簡単です。

于 2016-01-06T07:23:44.517 に答える
0

次の変更を加えることで、今すぐ機能させることができました。誰かがこれが役に立つと思った場合に備えて投稿しています。

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

ありがとう!

于 2015-02-27T19:33:38.043 に答える