在PreSaveAction方法

时间:2015-11-19 11:56:07

标签: javascript sharepoint

我的要求是从其他列表中获取值并将其设置为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");
 }

1 个答案:

答案 0 :(得分:-1)

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

由于您正在使用jQuery,因此可以使用jQuery提供的deferred / promise轻松实现。

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");
}
相关问题