在typescript中执行静态代码(扩展静态对象)

我想用静态函数扩展我的UserModel.model对象。 我该怎么做? 正如yu可以在下面看到的,我的UserModel.model是一个mongoose模型对象,但是我需要给这个对象添加方法,重点是:我不知道怎么写它。 我不能像Java / C#中那样使用一个静态的区域来执行静态代码,我没有find写它的方法。 你做?

例如,我想添加诸如exists()connect()等方法

///<reference path='../../lib/def/defLoader.d.ts'/> import model = require('./Model'); export module Models { export class UserModel extends model.Models.Model implements model.Models.IModel{ /** * Name of the model. * MUST start by uppercase letter! */ private static modelName: string = 'User'; /** * Public readable schema as object. */ public static schema: any = { /** * User Login */ login: { type: 'string', minLength: 4, maxLength: 16, required: true, notEmpty: true, unique: true } }; /** * Private schema as mongoose.Schema type. */ private static _schema: mongoose.Schema = new mongoose.Schema(UserModel.schema); /** * The mongoose model that uses the mongoose schema. */ public static model: mongoose.Model<any> = mongoose.model(UserModel.modelName, UserModel._schema); /** ************************************************************************************************* *********************************** Public (Helpers) ******************************************** ************************************************************************************************* */ /** * Name of the model. * MUST start by uppercase letter! */ public modelName: string; /** * Contains the static value of the public schema as object. * It's a helper to always get the schema, from instance or static. */ public schema: mongoose.Schema; /** * Contains the static value of the object used to manipulate an instance of the model. * It's a helper to always get the model, from instance or static. */ public model: mongoose.Model<any>; /** * Use static values as instance values. */ constructor(){ super(); // Use static values as instance values. this.modelName = UserModel.modelName; this.schema = UserModel.schema; this.model = UserModel.model; } /** ************************************************************************************************* *********************************** Extended methods ******************************************** ************************************************************************************************* */ } } 

谢谢。

我build议你使用标准的MongooseJS方法,通过使用一个Schema实例的statics属性添加一个静态方法到一个模型,如下所示。

 module MongooseDemo { export class Demo { public static LoginSchema: any = { /* User Login */ login: { type: 'string', minLength: 4, maxLength: 16, required: true, notEmpty: true, unique: true } }; public static Model : any; // static store for data constructor() { // there is not a concept of a static constructor in // typescript, so static initialization code may be better else // where } } // I've made this an anonymous function that is private to the module // It's executed immediately and initializes the "static" values of the // class. this code could be moved into a static method of course and // and called directly in the same way (()=> { // create the schema defined var schema : any = mongoose.Schema(Demo.LoginSchema); // add the statics before creating the model // http://mongoosejs.com/docs/guide.html#statics schema.statics.exists = () => { // here's a static method }; // create the model Demo.Model = mongoose.Model('Demo', schema); })(); } 

由于JavaScript或Typescript中没有静态构造函数的概念,因此我在MongooseDemo模块中创build了一个匿名函数来自动初始化类的静态字段。 你当然可以移动代码,但一般的概念应该工作。

或者,您可以直接将静态方法添加到模型中(在匿名方法中):

 Demo.Model.exists2 = () => { // something different }; 

在Mongoose中添加一个类方法/静态的实际代码非常简单,因为它只是直接将所有的函数(和属性)从Schema对象实例的statics属性复制到新的Model 。 所以,我提供的第二个例子在function上与我提出的第一种方法相同。