Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a JavaScript function that is inside a namespace

Tags:

javascript

If I had something like:

App = {
    editingMode: function ()
    {
        function setEditingMode(entityID) {
            $('#editingMode').val('1');
            $.ajax({
                type: "POST",
                url: "/Organisations/Manage/LockEntity/",
                data: "entityID=" + entityID
            });
        }
        function setEditingModeOff(entityID) {
            if ($("#myform").valid() == true)
            {
                $('#editingMode').val('0');
                $.ajax({
                    type: "POST",
                    url: "/Organisations/Manage/UnlockEntity/",
                    data: "entityID=" + entityID
                });
            }
        }
    }
};

How would I run one of the inner functions?

App.editingMode(); but then what would I do to get at the setEditingMode ??

like image 780
Cameron Avatar asked Oct 10 '11 11:10

Cameron


1 Answers

You can use a different structure to accomplish what you want. I don't know if it would break something else you are using, so I'll just give you an example. I'm not sure this is the actual solution, please take a look and tell me if that's not what you need. If you use:

 var App = {
                editingMode:
                {
                    setEditingMode: function(entityID) {
                        $('#editingMode').val('1');
                        $.ajax({
                            type: "POST",
                            url: "/Organisations/Manage/LockEntity/",
                            data: "entityID=" + entityID
                        });
                    },
                    setEditingModeOff: function(entityID) {
                        if ($("#myform").valid() == true)
                        {
                            $('#editingMode').val('0');
                            $.ajax({
                                type: "POST",
                                url: "/Organisations/Manage/UnlockEntity/",
                                data: "entityID=" + entityID
                            });
                        }
                    }
                }
            };

you can call editingMode's methods like this:

App.editingMode.setEditingModeOff(1);

Notice that they will still be encapsulated within App, you don't necessarily move them to the global scope.

like image 91
Nikoloff Avatar answered Nov 01 '22 08:11

Nikoloff