在PreSaveAction方法之后执行的ECMAScript回调方法
ECMAScript Callback method executed after PreSaveAction method
我的要求是从另一个列表中获取值,并将其设置为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");
}
相关文章:
- electronic BrowserWindow的最小高度和宽度在hide()show()方法之后不起作用
- 有没有任何方法可以将控制器从文件加载到ui路由器$stateProvider中
- 数组在递归方法中设置为null
- 打破承诺链的好方法是什么
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- 使用“;这个“;JavaScript原型方法中的关键字
- 序列化数据属性中对象的最可靠方法
- 使用Objective-C的JavaScript注入方法
- 有没有一种方法可以添加相同的项目val=“0”;4〃;到JavaScript中数组的每个对象
- 有没有一种方法可以防止img get请求使用css或js发生
- Javascript,有没有一种方法可以将数组写成没有逗号或空格的单个文本字符串
- 好的 ECMAScript 2015 方法来包装 CasperJS
- ECMAscript 是否提供任何构造函数的方法?
- 为什么 ECMAScript 6 包含 Set 对象,但没有提供比较它们相等的方法
- 在类中使用Ecmascript 6箭头函数作为方法的正确方法是什么
- 在PreSaveAction方法之后执行的ECMAScript回调方法
- EcmaScript:将所有方法导入到局部作用域中
- ECMAScript的承诺.all方法与jQuery.Deferred一起工作.为什么
- 使用ECMASCRIPT 6生成器/函数求和数组的最佳方法是什么
- 为什么ECMAScript 5中Object上没有forEach方法