Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在typescript中执行静态代码(扩展静态对象)_Node.js_Static_Typescript - Fatal编程技术网

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

Node.js 在typescript中执行静态代码(扩展静态对象),node.js,static,typescript,Node.js,Static,Typescript,我想用静态函数扩展UserModel.model对象。我怎么做? 正如你在下面看到的,我的UserModel.model是一个mongoose模型对象,但我需要向这个对象添加方法,重点是:我不知道如何编写它。我不能像Java/C#那样使用静态{}区域来执行静态代码,我找不到编写它的方法。你知道吗 例如,我想添加一些方法,如exists(),connect(),等等 ///<reference path='../../lib/def/defLoader.d.ts'/> import

我想用静态函数扩展UserModel.model对象。我怎么做? 正如你在下面看到的,我的UserModel.model是一个mongoose模型对象,但我需要向这个对象添加方法,重点是:我不知道如何编写它。我不能像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 ********************************************
         *************************************************************************************************
         */



    }
}
///
导入模型=需要('./模型');
导出模块模型{
导出类UserModel扩展model.Models.model实现model.Models.IModel{
/**
*模型的名称。
*必须以大写字母开头!
*/
私有静态modelName:string='User';
/**
*作为对象的公共可读架构。
*/
公共静态架构:任意={
/**
*用户登录
*/
登录:{
键入:“字符串”,
最小长度:4,
最大长度:16,
要求:正确,
是的,
独一无二:真的
}
};
/**
*私有架构为mongoose.schema类型。
*/
私有静态模式:mongoose.schema=newmongoose.schema(UserModel.schema);
/**
*使用mongoose模式的mongoose模型。
*/
公共静态模型:mongoose.model=mongoose.model(UserModel.modelName,UserModel.\u schema);
/**
*************************************************************************************************
***********************************公众(家务助理)********************************************
*************************************************************************************************
*/
/**
*模型的名称。
*必须以大写字母开头!
*/
公共模型名:字符串;
/**
*包含作为对象的公共架构的静态值。
*它是始终从实例或静态获取模式的助手。
*/
公共模式:mongoose.schema;
/**
*包含用于操纵模型实例的对象的静态值。
*它是始终从实例或静态获取模型的助手。
*/
公共模型:猫鼬模型;
/**
*使用静态值作为实例值。
*/
构造函数(){
超级();
//使用静态值作为实例值。
this.modelName=UserModel.modelName;
this.schema=UserModel.schema;
this.model=UserModel.model;
}
/**
*************************************************************************************************
***********************************扩展方法********************************************
*************************************************************************************************
*/
}
}

谢谢。

我建议您使用标准的MongooseJS方法,通过使用
模式
实例的
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
模块中创建了一个匿名函数来自动初始化类的静态字段。当然,您可以移动代码,但是一般的概念应该是可行的

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


在Mongoose中添加类方法/静态的实际代码非常简单,因为它只是将
模式
对象实例的
静态
属性中的所有函数(和属性)直接复制到新的
模型
。因此,我提供的第二个示例在功能上与我提出的第一种方法相当。

您能否创建一个简单的示例来说明您试图实现的目标?由于参考文献太多,太复杂了。反正我试过了。编辑主帖子。Stackoverflow拒绝链接。。。太长了,即使是像Tiny Link这样的其他应用程序。我想做的是添加方法:UserModel.model.exists=function(){}为什么不按照MongooseJS希望的方式向模型添加静态数据呢?这确实有效,谢谢。我的问题更多的是用TypeScript的方式来写,我试着用“function”关键字来代替“()”。TS非常新:)我尝试了两种方法,最好的是第二种,因为调试,如果您犯了某种错误,根据IDE的不同,所有内容都将是红色的。(WebStorm 7.0.3),最好使用第二种方法,并使每个方法不在同一块中。
Demo.Model.exists2 = () => {
    // something different              
};