mirror of
https://github.com/wassname/DefinitelyTyped.git
synced 2026-09-02 12:00:51 +08:00
Merge pull request #965 from fullflavedave/master
Meteor definition updates and README
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
#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).
|
||||
|
||||
##Referencing Meteor type definitions in your app
|
||||
- Place the meteor.d.ts file in a directory (maybe `<app root dir>/lib/typescript`)
|
||||
- Add `/// <reference path='../lib/typescript/meteor.d.ts'/>` to the top of any TypeScript file
|
||||
|
||||
This will make these typed Meteor variables/objects available across your application:
|
||||
|
||||
- Meteor
|
||||
- Session
|
||||
- Deps
|
||||
- Accounts
|
||||
- Match
|
||||
- Computation
|
||||
- Dependency
|
||||
- EJSON
|
||||
- HTTO
|
||||
- Email
|
||||
- Assets
|
||||
- DPP
|
||||
|
||||
*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:
|
||||
|
||||
/// <reference path='../../lib/typescript/meteor.d.ts'/>
|
||||
|
||||
declare var Template: {
|
||||
newPosts: IMeteorViewModel;
|
||||
bestPosts: IMeteorViewModel;
|
||||
postsList: IMeteorViewModel;
|
||||
comment: IMeteorViewModel;
|
||||
commentSubmit: IMeteorViewModel;
|
||||
notifications: IMeteorViewModel;
|
||||
postPage: IMeteorViewModel;
|
||||
postEdit: IMeteorViewModel;
|
||||
postItem: IMeteorViewModel;
|
||||
postNew: IMeteorViewModel;
|
||||
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).
|
||||
|
||||
|
||||
##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:
|
||||
|
||||
module PostsModel {
|
||||
export var Posts = new Meteor.Collection('posts');
|
||||
};
|
||||
|
||||
this.PostsModel = PostsModel;
|
||||
|
||||
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:
|
||||
|
||||
PostsModel.Posts.findOne(Session.get('currentPostId'));
|
||||
|
||||
|
||||
##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/").
|
||||
|
||||
- Sample Site: <http://microscopic-typescript.meteor.com/>
|
||||
- Code (TypeScript and transpiled JS): <https://github.com/fullflavedave/MicroscopicTypeScript>
|
||||
|
||||
##Meteor package
|
||||
There will hopefully be a Meteor package soon listed on [Atmosphere](http://atmosphere.meteor.com "http://atmosphere.meteor.com") that can be easily added using `mrt add typescript`.
|
||||
Vendored
+28
-15
@@ -89,19 +89,9 @@ interface IMeteor {
|
||||
/************
|
||||
* Accounts *
|
||||
************/
|
||||
user(): {
|
||||
_id: string;
|
||||
username: string;
|
||||
emails?: {
|
||||
address: string;
|
||||
verified: boolean;
|
||||
}
|
||||
profile?: Object;
|
||||
services?: Object;
|
||||
createdAt?: number;
|
||||
};
|
||||
user(): IMeteorUser;
|
||||
userId(): string;
|
||||
users: IMeteorCollection;
|
||||
users: IMeteorUserCollection;
|
||||
loggingIn(): boolean;
|
||||
logout(callback?: Function): void;
|
||||
loginWithPassword(user: Object, password: string, callback?: Function): void;
|
||||
@@ -191,8 +181,8 @@ interface IMeteor {
|
||||
***************/
|
||||
interface IMeteorCollection {
|
||||
find(selector?, options?: Object): IMeteorCursor;
|
||||
findOne(selector, options?: Object): Object;
|
||||
insert(doc, callback?: Function): number;
|
||||
findOne(selector, options?: Object): any;
|
||||
insert(doc: Object, callback?: Function): string;
|
||||
update(selector, modifier, options?: Object, callback?: Function): void;
|
||||
remove(selector, callback?: Function): void;
|
||||
allow(options: Object): boolean;
|
||||
@@ -244,7 +234,7 @@ interface IMeteorManager {
|
||||
created(callback: Function): void;
|
||||
destroyed(callback: Function): void;
|
||||
events(eventMap: {[eventType: string]: Function}): void;
|
||||
helpers(helpers: Object): Object;
|
||||
helpers(helpers: Object): any;
|
||||
preserve(selector: Object): void;
|
||||
}
|
||||
|
||||
@@ -288,6 +278,29 @@ interface IMeteorHandle {
|
||||
/**************************
|
||||
* Accounts and Passwords *
|
||||
**************************/
|
||||
interface IMeteorUser {
|
||||
_id?: string;
|
||||
username?: string;
|
||||
emails?: {
|
||||
address: string;
|
||||
verified: boolean;
|
||||
}
|
||||
profile?: any;
|
||||
services?: any;
|
||||
createdAt?: number;
|
||||
}
|
||||
|
||||
interface IMeteorUserCollection {
|
||||
find(selector?, options?: Object): IMeteorCursor;
|
||||
findOne(selector, options?: Object): IMeteorUser;
|
||||
insert(doc: IMeteorUser, callback?: Function): IMeteorUser;
|
||||
update(selector, modifier, options?: Object, callback?: Function): void;
|
||||
remove(selector, callback?: Function): void;
|
||||
allow(options: Object): boolean;
|
||||
deny(options: Object): boolean;
|
||||
ObjectID(hexString?: string): Object;
|
||||
}
|
||||
|
||||
interface IMeteorAccounts {
|
||||
config(options: {
|
||||
sendVerificationEmail?: boolean;
|
||||
|
||||
Reference in New Issue
Block a user