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

时间:2013-12-30 13:34:23

标签: node.js static typescript

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

例如,我想添加 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 ********************************************
         *************************************************************************************************
         */



    }
}

感谢。

1 个答案:

答案 0 :(得分:2)

我建议您使用标准的MongooseJS方法,通过使用statics实例的Schema属性向模型添加静态方法,如下所示。

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模块中创建了一个匿名函数来自动初始化类的静态字段。您当然可以移动代码,但一般概念应该有效。

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

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

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