연속적으로 3 번 기능을 실행하기 위해 지연된 지연법을 적용하는 방법 ??

sharepoint.stackexchange https://sharepoint.stackexchange.com/questions/116306

  •  29-09-2020
  •  | 
  •  

문제

연속으로 3 번 같은 기능을 실행하려고 시도하지만 그 일을하는 동안 오류를 가져 오십시오.분명히 스크립트는 실행보다 빠르게 실행됩니다.코드 :

$("document").ready(function () {
    WebInherited();
    CreateGroup("Group1", "Description group 1");
    CreateGroup("Group2", "Description group 2");
    CreateGroup("Group3", "Description group 3");
});

function WebInherited() {
    var c = new SP.ClientContext();
    this.w = c.get_web();
    c.load(w, "HasUniqueRoleAssignments");
    c.executeQueryAsync(
        Function.createDelegate(this, this.fxLoad),
        Function.createDelegate(this, this.fxFail)
        );
}

function fxLoad() {
    console.log("hasUniqueRoleAssignments == " +   w.get_hasUniqueRoleAssignments());
    if (!w.get_hasUniqueRoleAssignments()) {
        console.log("Obtaining web's permission properties...");
        var ctx = new SP.ClientContext();
        web = ctx.get_web();
        ctx.load(web, "RoleAssignments", "RoleDefinitions");
        ctx.executeQueryAsync(
            Function.createDelegate(this, this.fxBreak(web)),
            Function.createDelegate(this, this.fxFail)
            );
    }
}

function fxBreak(web) {
    console.log("Breaking Inheritance...");
    web.breakRoleInheritance(false, true);

}

function fxFail() {
    console.log("faal");
}

function CreateGroup(sName, sDescr) {
    console.log("Loading site group data and checking if " + sName + " should be created...");
    groupName = sName;
    groupDescr = sDescr;
    var ctx = new SP.ClientContext();
    web = ctx.get_web();

    //group collection
    groups = web.get_siteGroups();

    //checken of group al bestaat
    ctx.load(groups);
    ctx.executeQueryAsync(
        Function.createDelegate(this, this.fxCheckIfExists),
        Function.createDelegate(this, this.fxFail)
    );    
}

function fxCheckIfExists() {
    console.log("groups query succeeded");
    var grpEnum = groups.getEnumerator();
    var cnt = 0;
    while (grpEnum.moveNext()) {
        var grpItem = grpEnum.get_current();
        console.log(grpItem.get_title());
        if (grpItem.get_title() == groupName) {
            cnt += 1;
            console.log("Group " + groupName + " already exists.")
            break;
        }
    }
    if (cnt == 0) {
        console.log("Group " + groupName + " does not exist")
        fxCreate(groupName, groupDescr);
    }
}

function fxCreate(groupName, groupDescr) {
    console.log("Creating group " + groupName + "...");
    var ctx = new SP.ClientContext();
    web = ctx.get_web();
    var groups = web.get_siteGroups();

    //info nieuwe group
    var infoGroup = new SP.GroupCreationInformation;
    infoGroup.set_title(groupName);
    infoGroup.set_description(groupDescr);

    //nwe group toevoegen aan group collection
    var oGroup = groups.add(infoGroup);

    //rol definieren, 1073741827=Contributor
    var rdContribute = web.get_roleDefinitions().getById(1073741827);

    //role collection ophalen en nwe rol toevoegen
    var collRoleDefBindings =   SP.RoleDefinitionBindingCollection.newObject(ctx);
    collRoleDefBindings.add(rdContribute);
    var assignments = web.get_roleAssignments();
    var raContribute = assignments.add(oGroup, collRoleDefBindings);

    //group creeren in ctx
    ctx.load(oGroup);
    ctx.executeQueryAsync(
        function () {
            console.log("group " + groupName + " gecreerd!")
        },
        function () {
            console.log("group " + groupName + " creeren mislukt...");
        });
}
.

기본적으로 내가 원하는 것은 : 1) 고유 한 도너스가 있는지 확인하십시오. 2) 아직 존재하지 않으면 3 개의 새 그룹을 만듭니다.

그러나 CreateGroup-Function을 다음과 같이 3 번 실행하지 않습니다.이전 기능이 완전히 완성 된 기능을 수행 할 때까지 펑션의 실행으로 어떻게 기다릴 수 있습니까?

모든 도움이 높아질 것입니다 !!!!

도움이 되었습니까?

해결책

jQuery 약속을 사용하십시오.기본 구현 :

var deferred = $.Deferred();

deferred.then(function() {
//do callback to check unique permissions
}).then(function(){
//create groups
});
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 sharepoint.stackexchange
scroll top