How can I use Global variable across multiple functions in JavaScript?

JonS

I have a piece of code which is intended to check the permission level and group membership of a use and launch a dialog box if the user has the correct permissions to access that section of the site.

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();
            }
        }

Unfortunately when it reaches the end this section the variables HasPermission and isGroupMember are still undefined so the dialogue launches automatically for every user. I have a feeling I have misused the .this keywords and this is a scoping error but I am not expert enough in JS to know for certain or be able to fix it. Can anyone tell me exactly what I've done wrong and how to fix it please?

lshas

You are performing async functions, which means the rest of the code will keep executing even though the stuff you have started first are not completed yet. You will have to call the CheckUserPermissions after onPermissionsSuccessMethod and the OnQuerySucceeded function has completed.

In addition to this the HasPermission and isGroupMember variables are local to the RequestNewSite function, which means they are out of the scope of the CheckUserPermissions function.

    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 [email protected] 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 [email protected] 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 [email protected].");
        }
        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 [email protected].");
        }
        else
        {
            showDialogue();
            document.getElementById("next-stage").focus();
        }
    }

This code should work.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I break a variable definition across multiple lines in a Makefile without spaces?

From Dev

How can I use global functions in Angularjs Protractor?

From Dev

How can I declare an extension with global scope across the solution

From Dev

Can I apply const qualifier to global variable for selective functions?

From Dev

How do I create a global variable in R which can be used within functions and sql statements?

From Dev

Variable Referenced before Assignment Error - How to use variables across multiple functions?

From Dev

Where can I store information to use across different functions

From Dev

html/javascript/php: how do i use a "global variable"?

From Dev

inline variable emulation: can I use global static references to enforce the initialization order of static variable in functions?

From Dev

How can I use ccache with gcc -march=native across multiple architectures?

From Dev

How can i check if there is a global or local variable

From Dev

How can I write to a global variable in Aurelia?

From Dev

How can i use global functions in Vue?

From Dev

How to Assign value to global variable and use it in other functions in jquery/javascript?

From Dev

How can I use a variable's value in a variable name in JavaScript?

From Dev

How can I break a variable definition across multiple lines in a Makefile without spaces?

From Dev

How to use Tkinter's 'file' variable across different functions in Python?

From Dev

Variable Referenced before Assignment Error - How to use variables across multiple functions?

From Dev

Can I use variable functions with language constructs?

From Dev

How can I use multiple conditionals and match to create a new variable?

From Dev

How do I use a variable from another function without making the variable global in JavaScript?

From Dev

how to use a global variable inside a function in javascript?

From Dev

How can I perform a global replace on a variable?

From Dev

How can I implement per-project appSettings and global appSettings across multiple Visual Studio projects?

From Dev

How to return the same variable across multiple functions

From Dev

How can I create multiple functions with variable in name?

From Dev

How can I lock a row, and use the lock across multiple transactions?

From Dev

Global variable? Sharing variables across multiple scripts?

From Dev

How can I make this javascript function not use a global variable?

Related Related

  1. 1

    How can I break a variable definition across multiple lines in a Makefile without spaces?

  2. 2

    How can I use global functions in Angularjs Protractor?

  3. 3

    How can I declare an extension with global scope across the solution

  4. 4

    Can I apply const qualifier to global variable for selective functions?

  5. 5

    How do I create a global variable in R which can be used within functions and sql statements?

  6. 6

    Variable Referenced before Assignment Error - How to use variables across multiple functions?

  7. 7

    Where can I store information to use across different functions

  8. 8

    html/javascript/php: how do i use a "global variable"?

  9. 9

    inline variable emulation: can I use global static references to enforce the initialization order of static variable in functions?

  10. 10

    How can I use ccache with gcc -march=native across multiple architectures?

  11. 11

    How can i check if there is a global or local variable

  12. 12

    How can I write to a global variable in Aurelia?

  13. 13

    How can i use global functions in Vue?

  14. 14

    How to Assign value to global variable and use it in other functions in jquery/javascript?

  15. 15

    How can I use a variable's value in a variable name in JavaScript?

  16. 16

    How can I break a variable definition across multiple lines in a Makefile without spaces?

  17. 17

    How to use Tkinter's 'file' variable across different functions in Python?

  18. 18

    Variable Referenced before Assignment Error - How to use variables across multiple functions?

  19. 19

    Can I use variable functions with language constructs?

  20. 20

    How can I use multiple conditionals and match to create a new variable?

  21. 21

    How do I use a variable from another function without making the variable global in JavaScript?

  22. 22

    how to use a global variable inside a function in javascript?

  23. 23

    How can I perform a global replace on a variable?

  24. 24

    How can I implement per-project appSettings and global appSettings across multiple Visual Studio projects?

  25. 25

    How to return the same variable across multiple functions

  26. 26

    How can I create multiple functions with variable in name?

  27. 27

    How can I lock a row, and use the lock across multiple transactions?

  28. 28

    Global variable? Sharing variables across multiple scripts?

  29. 29

    How can I make this javascript function not use a global variable?

HotTag

Archive