Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

  • I cannot control the order of the script inclusion, as it's dynamically generated by the application.

  • I don't want to migrate the whole code to the Microsoft's pattern, as it's quite useless and hard to read. And as I migrate to typescript, I even can't control the namespacing output.

  • I also don't want to introduce an alternative namespace to exclude the Ajax's ones, because it will introduce some confusion to all the team.

like image 956
Steve B Avatar asked May 20 '13 15:05

Steve B


1 Answers

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
like image 152
Steve B Avatar answered Oct 25 '22 21:10

Steve B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!