如何在JavaScript中的多个函数中使用全局变量?

时间:2016-04-01 12:43:04

标签: javascript variables sharepoint-2013

我有一段代码用于检查用户的权限级别和组成员身份,如果用户具有访问该站点部分的正确权限,则启动对话框。

function bindSettingsButton() {
            $("#mt-ngw-personalsettings").on("click", function() {
                RequestNewSite();
            });
        }

        function RequestNewSite()   {
            var HasPermission = false;
            var isGroupMember = false;
            CheckCurrentUserMembership();
            CheckUserHasEditPermissions();
            CheckUserPermissions();
        }

        function CheckCurrentUserMembership() {
            var clientContext = new SP.ClientContext.get_current();
            this.currentUser = clientContext.get_web().get_currentUser();
            clientContext.load(this.currentUser);

            this.userGroups = this.currentUser.get_groups();
            clientContext.load(this.userGroups);
            clientContext.executeQueryAsync(OnQuerySucceeded, OnQueryFailed);
        }

        function OnQuerySucceeded() {
            var isMember = false;
            var groupsEnumerator = userGroups.getEnumerator();
            while (groupsEnumerator.moveNext()) {
                var group = groupsEnumerator.get_current();
                if(group.get_title() == "Create Site OptOut") {
                isMember = true;
                this.isGroupMember = true;
                break;
                }
            }
        }

        function OnQueryFailed() 
        {
            alert("Couldn't check user group membership. Please contact  to resolve this issue.");
        }

        function CheckUserHasEditPermissions()  {
            context = new SP.ClientContext.get_current();
            web = context.get_web();
            this._currentUser = web.get_currentUser();
            this._theList = web.get_lists().getByTitle('siterequests');
            context.load(this._currentUser);
            context.load(this._theList, 'EffectiveBasePermissions')
            context.executeQueryAsync(Function.createDelegate(this, this.onPermissionsSuccessMethod), Function.createDelegate(this, this.onPermissionsFailureMethod));
        }

        function onPermissionsSuccessMethod(sender, args)   {
            if (this._theList.get_effectiveBasePermissions().has(SP.PermissionKind.editListItems))
            {
                this.HasPermission = true;
            }
            else
            {
                this.HasPermission = false;
            }
        }

        function onPermissionsFailureMethod()
            {
                alert("Couldn't check permissions. Please contact  to resolve this issue.");
            }

        function CheckUserPermissions() {
            if(this.isGroupMember == true)
            {
                alert("You do not have permission to create sites. If you believe you should have access to this functionality, please contact .");
            }
            else if(this.HasPermission == false)
            {
                alert("You do not have permission to create sites. If you believe you should have access to this functionality, please contact .");
            }
            else
            {
                showDialogue();
                document.getElementById("next-stage").focus();
            }
        }

不幸的是,当它到达此部分时,变量HasPermission和isGroupMember仍未定义,因此对话框会自动为每个用户启动。 我有一种感觉,我误用了.this关键字,这是一个范围错误,但我在JS中不够专业,无法确定或能够解决它。谁能告诉我到底我做错了什么以及如何解决它?

2 个答案:

答案 0 :(得分:1)

$("#mt-ngw-personalsettings").on("click", function() {
  RequestNewSite();
});

如果您希望使用RequestNewSite作为构造函数,则需要使用new来分配它。如果您作为函数调用,则不会创建对象(因此也就是状态)。

此外,所有类型的成员都需要在this上明确创建。

所以

function RequestNewSite()   {
  var HasPermission = false;
    var isGroupMember = false;
    CheckCurrentUserMembership();
    CheckUserHasEditPermissions();
    [...]

需要

function RequestNewSite()   {
  this.HasPermission = false;
  this.isGroupMember = false;
  this.CheckCurrentUserMembership();
  this.CheckUserHasEditPermissions();
  [...]

答案 1 :(得分:1)

您正在执行异步功能,这意味着即使您先启动的内容尚未完成,其余代码也会继续执行。 您必须在CheckUserPermissions之后致电onPermissionsSuccessMethodOnQuerySucceeded功能已完成。

除此之外,HasPermissionisGroupMember变量是RequestNewSite函数的本地变量,这意味着它们不在CheckUserPermissions函数的范围内。

    var HasPermission = false;
    var isGroupMember = false;
    var CompletedCallbacks = 0;
    function bindSettingsButton() {
        $("#mt-ngw-personalsettings").on("click", function() {
            RequestNewSite();
        });
    }

    function RequestNewSite()   {
        CheckCurrentUserMembership();
        CheckUserHasEditPermissions();
    }

    function CheckCurrentUserMembership() {
        var clientContext = new SP.ClientContext.get_current();
        this.currentUser = clientContext.get_web().get_currentUser();
        clientContext.load(this.currentUser);

        this.userGroups = this.currentUser.get_groups();
        clientContext.load(this.userGroups);
        clientContext.executeQueryAsync(OnQuerySucceeded, OnQueryFailed);
    }

    function OnQuerySucceeded() {
        var isMember = false;
        var groupsEnumerator = userGroups.getEnumerator();
        while (groupsEnumerator.moveNext()) {
            var group = groupsEnumerator.get_current();
            if(group.get_title() == "Create Site OptOut") {
            isMember = true;
            isGroupMember = true;
            break;
            }
        }
        CompletedCallbacks++;
        CheckUserPermissions();
    }

    function OnQueryFailed() 
    {
        alert("Couldn't check user group membership. Please contact SPCOE@capita.co.uk to resolve this issue.");
    }

    function CheckUserHasEditPermissions()  {
        context = new SP.ClientContext.get_current();
        web = context.get_web();
        this._currentUser = web.get_currentUser();
        this._theList = web.get_lists().getByTitle('siterequests');
        context.load(this._currentUser);
        context.load(this._theList, 'EffectiveBasePermissions')
        context.executeQueryAsync(Function.createDelegate(this, this.onPermissionsSuccessMethod), Function.createDelegate(this, this.onPermissionsFailureMethod));
    }

    function onPermissionsSuccessMethod(sender, args)   {
        if (this._theList.get_effectiveBasePermissions().has(SP.PermissionKind.editListItems))
        {
            HasPermission = true;
        }
        else
        {
            HasPermission = false;
        }
        CompletedCallbacks++;
        CheckUserPermissions();
    }

    function onPermissionsFailureMethod()
        {
            alert("Couldn't check permissions. Please contact SPCOE@capita.co.uk to resolve this issue.");
        }

    function CheckUserPermissions() {
        if(CompletedCallbacks != 2) return;
        if(isGroupMember == true)
        {
            alert("You do not have permission to create sites. If you believe you should have access to this functionality, please contact SPOCOE@capita.co.uk.");
        }
        else if(HasPermission == false)
        {
            alert("You do not have permission to create sites. If you believe you should have access to this functionality, please contact SPOCOE@capita.co.uk.");
        }
        else
        {
            showDialogue();
            document.getElementById("next-stage").focus();
        }
    }

此代码应该有效。