Domanda

Sto lavorando su un'app di SharePoint in cui ho bisogno di eseguire SP.RequestExecutor.executeAsync() per afferrare alcune informazioni da una delle liste web dell'host.A seconda del metodo chiamare la chiamata OTATA, voglio fare qualcosa di diverso.

Unfortcontely, executor.executeAsync() non sta giocando come carino come $.ajax() in termini di utilizzo di $.when().done() ed eseguendo il codice dopo viene effettuata una chiamata ASYNC.Di seguito è riportato un codice se desideri vedere:

    load = function (){
        $.when(getEntries()).done(function () {
            ViewModels.Calendar.addEventSource(ko.utils.unwrapObservable(eventList));
        });
    }

    getEntries = function () {
        return executor.executeAsync({
            url: appweburl
                + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + LIST + "')/items?@target='" + hostweburl
                + "'&$select=Title,OData__x006e_ot5,qnlu,OData__x0066_x20"
                + "&$filter=OData__x0066_x20 eq '" + ViewModels.Person.user.userName() + "' "
                + "and qnlu ge DateTime'" + startDate().toString("yyyy-MM-dd") + "T00:00:00' "
                + "and qnlu le DateTime'" + endDate().toString("yyyy-MM-dd") + "T00:00:00' ",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: onGetEntriesSuccess,
            error: onoDataCallFailure 
        });
    },

    onGetEntriesSuccess = function (data) {
        var jsonObject = JSON.parse(data.body);
        $.each(jsonObject.d.results, function (index, item) {
            eventList.push(new Event(item.qnlu, item.OData__x006e_ot5));
        });
    },

    onoDataCallFailure = function (data, errorCode, errorMessage) {
        alert('Failed to get host site. Error:' + errorMessage);
    };
.

Qualsiasi idea / suggerimenti sarebbe apprezzata.

È stato utile?

Soluzione

Crea un nuovo .Deferred.Restituire differita. Prombe dalla chiamata.Chiama differita.Resolve o deferred.reject nei gestori di successo e guasto.

Altri suggerimenti

Per aggiungere alcuni dettagli alla risposta della Scot, potresti fare qualcosa del genere:

function load () {
    var call = getEntries();
    call.done(function (eventList) {
        ViewModels.Calendar.addEventSource(ko.utils.unwrapObservable(eventList));
    });
}

function getEntries () {
    // create the deferred object
    var def = new $.Deferred();

    executor.executeAsync({
        url: appweburl
            + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + LIST + "')/items?@target='" + hostweburl
            + "'&$select=Title,OData__x006e_ot5,qnlu,OData__x0066_x20"
            + "&$filter=OData__x0066_x20 eq '" + ViewModels.Person.user.userName() + "' "
            + "and qnlu ge DateTime'" + startDate().toString("yyyy-MM-dd") + "T00:00:00' "
            + "and qnlu le DateTime'" + endDate().toString("yyyy-MM-dd") + "T00:00:00' ",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: onGetEntriesSuccess,
        error: onoDataCallFailure 
    });

    function onGetEntriesSuccess (data) {
        var jsonObject = JSON.parse(data.body);
        var eventList = [];
        $.each(jsonObject.d.results, function (index, item) {
            eventList.push(new Event(item.qnlu, item.OData__x006e_ot5));
        });

        // resolve the deferred object
        // eventList will be passed as parameter to done
        def.resolve(eventList);
    }

    function onoDataCallFailure (data, errorCode, errorMessage) {
        // reject the deferred object            
        def.reject('Failed to get host site. Error:' + errorMessage);
    }

    // return the promise
    return def.promise();
}
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top