Mongoose 的实例方法中访问静态方法

方法比较简单,也比较粗糙和丑陋,就是通过构造函数来访问静态方法,大致如下:

1
2
3
4
5
6
7
8
9
WorkSpaceSchema.methods.getPrice = function(startTime, endTime){
// ...
var result = days * Math.floor(this.constructor.getPricePerDay(this.discountPrice || this.price, this.priceType));
// ...
};

WorkSpaceSchema.statics.getPricePerDay = function(price, priceType){
// code
};

看到这个解决方案后,第一想法是,Mongoose 在设计的时候,为什么没有考虑方法之间的访问这一情形呢,仔细一想,静态方法的设计初衷,只是为了通过对 Model 已有方法进行扩充和自定义,创建出一些特别的搜索方法之类,而如果想要创建在其它方法中调用的方法,更好的解决办法是写单独的模块,然后再导出和调用。

posted @ 2016-02-29 22:30  郑家好人  阅读(598)  评论(0编辑  收藏  举报