Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Modules/Classes in Typescript with Requirejs

I'm attempting to start using RequireJs from within MS CRM, but I'm confused as to how to use Require JS with my current TypeScript files.

Currently, each form in CRM has it's own Typescript file, that looks something like this:

// File Path .\_Contoso\scripts\Contact.ts
module Contoso {
    export class Contact {

        private static instance = new Contact();

        //#region Form Properties

        static fields = { }

        //#endregion Form Properties

        //#region onLoad / onSave

        static onLoad(): void {
            Contact.instance.onLoad();
        }

        private onLoad = (): void => { ...}

        static onSave(): void { Contact.instance.onSave(); }

        private onSave = (): void => { ... }

        //#endregion onLoad / onSave
    }
}

Each file may contain dependencies on one or more common files/classes:

// File Path .\_Abc\scripts\CommonLib.ts
module ABC_Corp {
    export class CommonLib {
        ... 
    }
}

// File Path .\_Abc\scripts\RestLib.ts
module ABC_Corp {
    export class RestLib {
        ... 
    }
}


// File Path .\_Abc\scripts\RoleLib.ts
module ABC_Corp {
    export class RoleLib {
        ... 
    }
}

These files all currently live in a VS Website Project. Whenever I save the ts files, it generates the JS and I deploy those files to CRM.

Now, enter RequireJS. Since the only supported hook in CRM for JS file execution to begin is the "OnLoad" and "onSave" events, I have created a Contoso.Require file that will read the require configuration from the CRM OnLoad function call, and then call the appropriate onLoad method on the form script. This is all working in order to load the main form JS, and call the onLoad function. My problem is I have to define in the CRM OnLoad Event, the required JS files, rather than having each file define what files are required by it. How do I define what files are required by each class/file?

like image 492
Daryl Avatar asked Nov 23 '25 05:11

Daryl


1 Answers

rather than having each file define what files are required by it.

You still need to do that. Because that is what is going to drive the ordering.

But now instead of using module (which are now called namespaces) use the import/export format to use true modules.

More

https://basarat.gitbooks.io/typescript/content/docs/project/modules.html

like image 177
basarat Avatar answered Nov 24 '25 23:11

basarat



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!