javascriptnamespacesmicrosoft-ajax

Type.registerNamespace is throwing errors when namespace already exists


In my corporate environment, we are using a lot of javascript. To simplify the management of all this script, and avoid naming collision, we adopted a javascript naming convention for namespaces, which is basically:

CompanyName.ProjectName.Area.XYZ.js

To create the namespaces, we are using the following pattern:

var Company;
(function (Company) {
    (function (Project) {
        (function (Area) {
            (function (XYZ) {

                function function1(args) {

                },
                function function2(args) {

                }
            })(Area.XYZ|| (Area.XYZ = {}));

        })(Project.Area || (Project.Area = {}));
        var Area = Project.Area;
    })(Company.Project || (Company.Project = {}));
    var Project = Company.Project;
})(Company || (Company = {}));

Which works fine (actually, this is the output of a TypeScript script).

However, I also have some scripts that use the Microsoft Ajax namespacing function, because this is required by out of control applications (javascript plugin).

I use this declaration :

Type.registerNamespace('CompanyName.ProjectName.Area');
CompanyName.ProjectName.Area.ABC = function() {
}
CompanyName.ProjectName.Area.ABC.prototype = {
    function1 : function (args) {

    },
    function2 : function (args) {

    }
}

But the call to Type.registerNamespace throws an error:

Sys.InvalidOperationException: Object Company already exists and is not a namespace

How can I properly combine both worlds? How can I solve my issue and make the warning disappears?


Solution

  • Some months later... I finally had to create two separate namespaces. One for MS Ajax rigid model, one for self created namespaces.

    CompanyName.ProjectName.Area
    CompanyNameAjax.ProjectName.Area