Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript with requireJs for coding modular way

define(["require", "exports", "js/models/home","templates/home/home.html"
], function(require, exports, __model__, __homeView__) {

    var model = __model__;
    var homeView=__homeView__;


}

I would like to write a .ts file which will generate a js file like this.

By compiling --module amd I can import a model and also reference the jquery, backboneJs or any other js files. But how can I import an externer html file as like requireJs does?

like image 892
Md. Asadul Islam Avatar asked Nov 05 '12 11:11

Md. Asadul Islam


People also ask

How do you make a TypeScript module?

A module can be created using the keyword export and a module can be used in another module using the keyword import . In TypeScript, files containing a top-level export or import are considered modules.

What are TypeScript modules?

In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module. Conversely, a file without any top-level import or export declarations is treated as a script whose contents are available in the global scope (and therefore to modules as well).

How add require in Javascript?

To include the Require. js file, you need to add the script tag in the html file. Within the script tag, add the data-main attribute to load the module. This can be taken as the main entry point to your application.


1 Answers

I put together a blog a while ago on require.js and Typescript.
http://blorkfish.wordpress.com/2012/10/23/typescript-organizing-your-code-with-amd-modules-and-require-js/
In order to import text files, you will need to reference text.js, and then use the text!<...your text file> syntax, as below. Using the require.config further simplifies the use of require:

require.config({
    baseUrl: '../',
    paths: {
        views: 'app/views',
        'text': 'lib/text',
    }
});

require([
    'text!views/MTodoCollectionView.html'],
    (MTodoCollectionViewSnippet) => {
        // 
    });
like image 128
blorkfish Avatar answered Oct 13 '22 09:10

blorkfish