在PreSaveAction方法之后执行的ECMAScript回调方法

ECMAScript Callback method executed after PreSaveAction method

本文关键字:方法 ECMAScript 回调 之后 PreSaveAction 执行      更新时间:2023-09-26

我的要求是从另一个列表中获取值,并将其设置为SharePoint列表形式,然后保存该项。

我写了ECMAScript和PreSaveAction()。然而,PreSaveAction()返回true是在ECMAScript回调方法之前执行的,这就是为什么当我单击Save按钮时,我的字段值没有被保存的原因。请注意,我的回调函数没有错误,我在警报中得到了正确的值。我不知道我哪里做错了。下面是我的代码供您参考。

function PreSaveAction() 
    { 
        ExecuteOrDelayUntilScriptLoaded(GetTotalAmtAlloted, "sp.js");
        return true;**//This line will execute before my callback function onListItemsLoadSuccess or onQueryFailed execute. Because of this, my field value not able to save it.**
    }

 function GetTotalAmtAlloted()
    {
        var clientContext = null;
        var web = null;
        var vID;
        clientContext = new SP.ClientContext.get_current();
        web = clientContext.get_web();
        var list = web.get_lists().getByTitle("Investment");
        var camlQuery = new SP.CamlQuery();
        var q = "<View><Query><Where><Eq><FieldRef Name='InvestmentSubject' /><Value Type='Text'>abc</Value></Eq></Where></Query></View>";
        camlQuery.set_viewXml(q);
        listItems = list.getItems(camlQuery);
        clientContext.load(listItems);
        clientContext.executeQueryAsync(onListItemsLoadSuccess,onQueryFailed);//After this line "return true" in PreSaveAction() will execute and then CallBackMethods will run.
}
function onListItemsLoadSuccess(sender, args) 
{
  $("input[Title='Total Amount']").val("1000");
}
 function onQueryFailed(sender,args)
 {
  alert("error");
 }

您正在执行异步函数调用,但希望它执行同步操作。相反,您必须等待异步函数完成,然后PreSaveAction才能返回true

由于您使用的是jQuery,因此使用jQuery提供的延迟/承诺可以很容易地实现这一点。

function PreSaveAction() { 
    //Get the promise from the function
    var promise = GetTotalAmtAlloted();
    //This is run when the promise is resolved
    promise.done(function(){
        return true;
    });
    //This is run when the promise is rejected
    promise.fail(funtion(){
    });
}

function GetTotalAmtAlloted() {
    var d = $.Deferred();
    var clientContext = null;
    var web = null;
    var vID;
    clientContext = new SP.ClientContext.get_current();
    web = clientContext.get_web();
    var list = web.get_lists().getByTitle("Investment");
    var camlQuery = new SP.CamlQuery();
    var q = "<View><Query><Where><Eq><FieldRef Name='InvestmentSubject' /><Value Type='Text'>abc</Value></Eq></Where></Query></View>";
    camlQuery.set_viewXml(q);
    listItems = list.getItems(camlQuery);
    //An object to pass to the success and failure handlers
    var data = {d: d, list: listItems}
    clientContext.load(listItems);
    //Execute the query and pass the data with our deferred object
    clientContext.executeQueryAsync(Function.createDelegate(data, onListItemsLoadSuccess), Function.createDelegate(data, onQueryFailed));//After this line "return true" in PreSaveAction() will execute and then CallBackMethods will run.
    //Return a promise that can either resolve or reject depending on if the async function is a success or failure
    return d.promise();    
}
function onListItemsLoadSuccess() {
    //Access the listItems
    var listItems = this.list;
    //Do something with the list items
    //On success, resolve the promise
    this.d.resolve();
}
function onQueryFailed() {
    //On failure, reject the promise
    this.d.reject("Something went wrong!");
    alert("error");
}