Merge pull request #989 from fullflavedave/master

README improvements
This commit is contained in:
Diullei Gomes
2013-09-03 14:58:26 -07:00

View File

@@ -1,6 +1,11 @@
#Meteor Type Definitions Usage Notes
In order to effectively write your Meteor app with TypeScript, there are a few extra things you will need to do in addition to simply referencing the Meteor type definition file and renaming all of your *.js files to *.ts (which alone will not work).
In order to effectively write a Meteor app with TypeScript, you will probably need to do these things:
- Reference the Meteor type definitions file (meteor.d.ts)
- Create a Template definition file
- Create Collections within a module or modules
##Referencing Meteor type definitions in your app
- Place the meteor.d.ts file in a directory (maybe `<app root dir>/lib/typescript`)
@@ -23,6 +28,7 @@ This will make these typed Meteor variables/objects available across your applic
*Please note that the Template variable is not automatically available. You need to follow the instructions below to use the Template variable.*
##Defining Templates
In order to call `Template.yourTemplateName.method`, you will need to create a simple TypeScript definition file that declares a Template variable containing a list of template view-models/managers of type IMeteorViewModel (or IMeteorManager, which is the same as IMeteorViewModel). A good place for this definition could be `<app root dir>/client/views/view-model-types.d.ts`. Here is an example of that file:
@@ -42,25 +48,29 @@ In order to call `Template.yourTemplateName.method`, you will need to create a s
header: IMeteorViewModel;
}
After you create this file, you may access the Template variable by declaring something like `/// <reference path='../view-model-types.d.ts'/>` at the top of any TypeScript file containing references to Template. Something like `Template.postsList.helpers()` would then transpile successfully (and have the benefits of typing).
After you create this file, you may access the Template variable by declaring something similar to `/// <reference path='../view-model-types.d.ts'/>` at the top of any TypeScript file containing references to Template. Something like `Template.postsList.helpers()` would then transpile successfully (and also have the benefits of typing).
##Defining Collections
In TypeScript, global variables are not allowed, and in a Meteor app, creating a local variable (using `var <varName>`) limits a variable's scope to the file. However, you will probably want to define variables, such as collections, that can be used across files. In the case of collections, one way to work around these limitations is to wrap each collection within a module, and then make the module globally accessible. Here is an example using posts.ts:
In TypeScript, global variables are not allowed, and in a Meteor app, creating a local variable (using `var <varName>`) limits a variable's scope to the file. However, you will probably want to define variables, such as collections, that can be used across multiple files. In the case of collections, one way to work around these limitations is to wrap the definitions of all collections within a module, and then make the module globally accessible. Here is an example (collections/models.ts):
module PostsModel {
module Models {
export var Posts = new Meteor.Collection('posts');
};
export var Comments = new Meteor.Collection('comments');
export var Notifications = new Meteor.Collection('notifications');
}
this.PostsModel = PostsModel;
this.Models = Models;
You can then access the Posts collection by placing `/// <reference path='../../../collections/posts.ts'/>` at the top of a TypeScript file. The code would look like this:
You can then access the Posts collection by placing something similar to `/// <reference path='../../../collections/models.ts'/>` at the top of a TypeScript file. The code within the file would look something like this:
PostsModel.Posts.findOne(Session.get('currentPostId'));
Models.Posts.findOne(Session.get('currentPostId'));
For organizational purposes, any additional code related to each Collection can be placed in a separate file per each collection. Alternatively, you could wrap each collection in its own module (e.g. PostsModel for posts, CommentsModel for comments).
##Reference app
A simple Meteor reference application created with TypeScript is listed below. It is based on the Microscope reference app in [Discover Meteor](http://www.discovermeteor.com/ "http://www.discovermeteor.com/").
Listed below is a simple Meteor reference application created with TypeScript is listed below. It is based on the Microscope reference app in [Discover Meteor](http://www.discovermeteor.com/ "http://www.discovermeteor.com/").
- Sample Site: <http://microscopic-typescript.meteor.com/>
- Code (TypeScript and transpiled JS): <https://github.com/fullflavedave/MicroscopicTypeScript>