Dynamic instantiation using external modules

Topics: Code Checkins, General, Language Specification
May 29, 2014 at 1:22 PM
Edited May 29, 2014 at 1:33 PM
We started using requirejs for our project and converting all our internal modules to external modules.

I'm having problems dynamically instanciating classes. We need to be able to instanciate classes using a simple dot notation string.

When all our modules were internal, they were accessible through the window object. Here's an example :
    // file - TestAction.ts
    module cb.action {
        export class TestAction {
            constructor() {}
Using the following function with className = "cb.action.TestAction" we could easily get a TestAction instance.
    var instantiateByName = function (className) {
        "use strict";
        var ClassName2 = className.split('.').reduce(function (current, name) {
            return current[name];
        }, window);
        return new ClassName2();
Now that we changed all our modules to external modules the TestAction.ts file now looks like this :
// file - TestAction.ts
export class TestAction {
    constructor() {}
2 main problems :
- Depending on the dot notation string I'm receiving, I need to import a different file. ( dynamically require a file )
- I need an equivalent to the instantiateByName function above since I can't use the window object anymore. 
Looking forward to having your inputs

( I posted this on http://stackoverflow.com/questions/23934580/how-do-dynamically-instantiate-using-typescripts-external-modules also will post the answer on both posts )