6

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 ??

1
  • 2
    AFAIK those functions are effectively private, you can't. Commented Oct 10, 2011 at 11:44

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I guess that would work. But it almost makes me wonder why bother with the namespacing at all :/
It's always useful, I don't think I can list the benefits in the number of characters allowed in a comment. When you have a large project with dozens of objects with dozens of methods, etc. you'll appreciate namespacing :)
0

You would need to return a reference to one or otherwise make it accessible from outside of the function, for example, attaching it to window (effectively making it a global) or the App object.

1 Comment

@Cameron Namespacing is still a good idea. It's better to only have App global then have dozens of global functions, objects, etc.
0

Although the answer given by 'Nikoloff' enables you to call those functions but I would point out to bit different (and probably better) approach. Following 'Module' pattern concept will not just solve your problem effectively but also allow you to have private variables. (Search for 'Module pattern in Javascript' and it will give you loads of online resources.)

App = {
    editingMode: function ()
    {       
        // Below var is no way directly accessible and act as private var
        var privateVar =5;
        // Below function is going to give access to private var
        function getPrivateVar(){
            return privateVar;
        }
        //Your functions - untouched
        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
                });
            }
        }
        return {
            setEditingMode: setEditingMode,
            setEditingModeOff: setEditingModeOff,
            getPrivateVar: getPrivateVar
        }
    }
};

App.editingMode().setEditingMode();
console.log(App.editingMode().getPrivateVar());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.