当我尝试在SP Online中保存时,函数返回undefined而不是true / false

时间:2016-11-24 11:39:51

标签: javascript jquery sharepoint sharepoint-online spservices

我试图基于此函数设置True / False标志,因此我可以设置PreSaveAction以返回正确的结果。基本上,此函数检查权限组以查看当前登录用户是否为该组的一部分,设置了true或false标志,以便save操作可以返回此函数的结果,但由于某种原因,它返回undefined。香港专业教育学院通过警报添加评论仅供参考。

var g_rtnVal  = false;

$(document).ready(function(){

    g_rtnVal = ValidateTaskApprover();
    alert(g_rtnVal); // returned undefined

});
// this fails at times because its being called on docReady and the page doesnt load in time for AssignedTo to read
function ValidateTaskApprover() { 
    //var rtnVal = false;
    $().SPServices({
        operation: "GetGroupCollectionFromUser",
        userLoginName: $().SPServices.SPGetCurrentUser(),
        async: false,
        completefunc: function (xData, Status) {
            var strAssignedTo = SPUtility.GetSPField('Assigned To').GetValue();
            if ($(xData.responseXML).find("Group[Name='"+strAssignedTo+"']").length == 1) {
                var rtnVal = true;
                alert("sequence 1: SUCCESS (true)" + rtnVal); //returns true
                return rtnVal;
                alert("this should not print");
            }
            else{
                alert("sequence 1: FAILURE (false)" + rtnVal);
                return rtnVal;
            }
        }
    });
} /* ValidateTaskApprover */

function PreSaveAction() {
    var bPreSaveReturn = false;
    bPreSaveReturn = ValidateTaskApprover();
    alert(bPreSaveReturn); // returns undefined
    alert(ValidateTaskApprover()); // returns undefined
    return false;
} /* PreSaveAction */

我解决了。我需要做的就是从completefunc回调中返回值:

function ValidateTaskApprover() { 
var rtnVal = false;
var strAssignedTo = SPUtility.GetSPField('Assigned To').GetValue();

$().SPServices({
    operation: "GetGroupCollectionFromUser",
    userLoginName: $().SPServices.SPGetCurrentUser(),
    async: false,
    completefunc: function (xData, Status) {
        if ($(xData.responseXML).find("Group[Name='"+strAssignedTo+"']").length == 1) {
            rtnVal = true;
        }
        else{
            alert("This task has been assigned to "+strAssignedTo+". You do not have the permissions to Approve it");
        }
    }
});
return rtnVal
}    
function PreSaveAction() {
return ValidateTaskApprover();  
}

0 个答案:

没有答案