我有一个SharePoint表单,我在可编辑的表格中添加了该表单,允许用户添加,编辑和删除子表单中与项目上的父项具有多对一关系的项目。
为了管理这一点,我重写了提交功能,并在提交表单之前运行以下使用AJAX和SharePoint Rest api的代码来处理子列表上的插入和更新。
提交覆盖的部分鳕鱼:
$.when(bulkInsert(arrayOfNew, parentId)
).then(function() {
$.when(bulkUpdate(arrayOfUpdates,parentId)
).then(function() {
alert("all done");
//run initial submit
});
});
bulkInsert和bulkUpdate的工作方式相同,问题在于最终嵌套的Rest / AJAX调用,因此只显示updateItems。
function bulkUpdate(updates, parentId) {
var deferred = $.Deferred();
var promises= [];
$.map(updates, function (item) {
promises.push(updateItem(parentId, item))
})
$.when.apply($, promises).then(function () { deferred.resolve(); });
return deferred.promise();
}
我相信我的代码是正确的。
要使用SharePoint Rest api更新项目,我必须首先获取项目的RequestDigest和REST URI,因此最终的ajax调用嵌套在函数的成功分支中,以便选择AJAX调用来获取项目。我已经尝试了几件事,但没有一件工作,所以我只会展示工作代码,希望有人可以告诉我如何添加所需的延迟代码以使脚本正常工作。
function updateItem(parentId, childItem) {
getItem(childItem.Id, function (data) {
//complete function
var updateItem = {
"__metadata": { "type": "SP.Data.ChildListItem" },
"LookupID": parentId,
"Field1": item.Field1,
"Field2": item.Field2,
"Field3": item.Field3 //etc.
};
$.ajax({
url: data.d.results[0].__metadata.uri,
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(updateItem),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "MERGE",
"If-Match": data.d.results[0].__metadata.etag
},
success: function (data) {
alert("update succesful");
//
},
error: function (data) {
alert(JSON.stringify(data));
}
});
}, function (data) {
//failure function
alert(JSON.stringify(data));
});
}
function getItem(childId, complete, failure) {
$.ajax({
url: (_spPageContextInfo.webServerRelativeUrl == "/" ? "" : _spPageContextInfo.webServerRelativeUrl) + "/_api/web/lists/getbytitle('ChildList')/items?select=ID&$filter=ID eq " + childId,
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
// Returning the results
alert("got item");
complete(data);
},
error: function (data) {
failure(data);
}
});
}
感谢您的帮助。
答案 0 :(得分:0)
updateItem()
不会返回承诺。
function updateItem(parentId, childItem) {
var def = $.Deferred();
getItem(childItem.Id, function (data) {
$.ajax({
success: function (data) {
def.resolve(true);
},
});
});
return def.promise();
}
答案 1 :(得分:0)
首先,主程序应简化如下:
bulkInsert(arrayOfNew, parentId).then(function() {
bulkUpdate(arrayOfUpdates, parentId).then(function() {
alert("all done");
//run initial submit
});
});
我不确定arrayOfUpdates
它来自哪里?它是由bulkInsert
生成的吗?
然后,bulkUpdate
可简化如下:
function bulkUpdate(updates, parentId) {
var promises = $.map(updates, function(item) {
return updateItem(parentId, item);
});
return $.when.apply($, promises);
}
然后,updateItem
必须返回一个承诺,并且可能会受益于内部重构,如下所示:
function updateItem(parentId, childItem) {
function getItem(id) {
//Note the `return`
return $.ajax({
url: (_spPageContextInfo.webServerRelativeUrl == "/" ? "" : _spPageContextInfo.webServerRelativeUrl) + "/_api/web/lists/getbytitle('ChildList')/items?select=ID&$filter=ID eq " + id,
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
});
}
function update(data) {
var requestData = {
"__metadata": { "type": "SP.Data.ChildListItem" },
"LookupID": parentId,
"Field1": item.Field1,
"Field2": item.Field2,
"Field3": item.Field3 //etc.
};
//Note the `return`
return $.ajax({
url: data.d.results[0].__metadata.uri,
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(requestData),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "MERGE",
"If-Match": data.d.results[0].__metadata.etag
}
});
}
//Closure-forming utility for success/error reporting
function reportAjaxStatus(prefix) {
return function (x, textStatus) {
var message = (prefix || '-') + (textStatus || '-');
alert(message);
}
}
//Master routine (simplify when debugged)
//Note the `return`
return getItem(childItem.Id)
.always(reportAjaxStatus('getItem: '))
.then(update)
.always(reportAjaxStatus('update: '));
}
这使得一个简洁易读的主程序可以控制所有成功/错误报告,并且可以在调试所有内容时轻松简化。