What is this angularjs code doing with regards the factory and its function
<p>This code was from a demo I was playing with using ASP.Net MVC and AngularJS together.  The discussion was on a youtube video <a href="https://www.youtube.com/watch?v=f67PFtrldGQ" rel="nofollow">here</a>, the source code is linked in the comments.  The portion of code in question was in the main JavaScript source file for the Angular module definition.  I'm familiar with AngularJS, but this syntax is something I don't understand. My guess is the function used by the factory definition is just being separated into different area, rather than all contained within the same block of code of the factory itself?</p>

<p>See below</p>

<p>Why would this function be defined with a wrapper around it like this (all within parenthesis)?  Its not within any controller or service.  Its simply in the same file as the module definition.</p>

<p>Is it just making the definition of the factory function separate from the factory definition itself?</p>

<p>Also, at the very end, what purpose would <code>(window.MyApp))</code>; serve?</p>

<p><strong>function in question:</strong></p>

<pre><code>(function (myApp) {
    var viewModelHelper = function ($http, $q, $window, $location) {

        var self = this;

        self.modelIsValid = true;
        self.modelErrors = [];

        self.resetModelErrors = function () {
            self.modelErrors = [];
            self.modelIsValid = true;
        }

        self.apiGet = function (uri, data, success, failure, always) {
            self.modelIsValid = true;
            $http.get(MyApp.rootPath + uri, data)
                .then(function (result) {
                    success(result);
                    if (always != null)
                        always();
                }, function (result) {
                    if (failure != null) {
                        failure(result);
                    }
                    else {
                        var errorMessage = result.status + ':' + result.statusText;
                        if (result.data != null &amp;&amp; result.data.Message != null)
                            errorMessage += ' - ' + result.data.Message;
                        self.modelErrors = [errorMessage];
                        self.modelIsValid = false;
                    }
                    if (always != null)
                        always();
                });
        }

        self.apiPost = function (uri, data, success, failure, always) {
            self.modelIsValid = true;
            $http.post(MyApp.rootPath + uri, data)
                .then(function (result) {
                    success(result);
                    if (always != null)
                        always();
                }, function (result) {
                    if (failure != null) {
                        failure(result);
                    }
                    else {
                        var errorMessage = result.status + ':' + result.statusText;
                        if (result.data != null &amp;&amp; result.data.Message != null)
                            errorMessage += ' - ' + result.data.Message;
                        self.modelErrors = [errorMessage];
                        self.modelIsValid = false;
                    }
                    if (always != null)
                        always();
                });
        }

        self.goBack = function () {
            $window.history.back();
        }

        self.navigateTo = function (path) {
            $location.path(MyApp.rootPath + path);
        }

        self.refreshPage = function (path) {
            $window.location.href = MyApp.rootPath + path;
        }

        self.clone = function (obj) {
            return JSON.parse(JSON.stringify(obj))
        }

        return this;
    };
    myApp.viewModelHelper = viewModelHelper;
}(window.MyApp));
</code></pre>

<p>Within the same module source file a 2nd module named 'commonModule' is defined and referenced as a dependency for the primary module. This commonModule the uses the viewModelHelper like so:</p>

<pre><code>commonModule.factory('viewModelHelper', function ($http, $q, $window, $location) {
    return MyApp.viewModelHelper($http, $q, $window, $location);
});
</code></pre>