@@ -710,6 +710,10 @@ Model.discriminator = function discriminator (name, schema) {
710
710
this . discriminators [ name ] = this . db . model ( name , schema , this . collection . name ) ;
711
711
this . discriminators [ name ] . prototype . __proto__ = this . prototype ;
712
712
713
+ // apply methods and statics
714
+ applyMethods ( this . discriminators [ name ] , schema ) ;
715
+ applyStatics ( this . discriminators [ name ] , schema ) ;
716
+
713
717
return this . discriminators [ name ] ;
714
718
} ;
715
719
@@ -2545,13 +2549,9 @@ Model.compile = function compile (name, schema, collectionName, connection, base
2545
2549
, collectionOptions
2546
2550
) ;
2547
2551
2548
- // apply methods
2549
- for ( var i in schema . methods )
2550
- model . prototype [ i ] = schema . methods [ i ] ;
2551
-
2552
- // apply statics
2553
- for ( var i in schema . statics )
2554
- model [ i ] = schema . statics [ i ] ;
2552
+ // apply methods and statics
2553
+ applyMethods ( model , schema ) ;
2554
+ applyStatics ( model , schema ) ;
2555
2555
2556
2556
model . schema = model . prototype . schema ;
2557
2557
model . options = model . prototype . options ;
@@ -2560,6 +2560,53 @@ Model.compile = function compile (name, schema, collectionName, connection, base
2560
2560
return model ;
2561
2561
} ;
2562
2562
2563
+ /*!
2564
+ * Register methods for this model
2565
+ *
2566
+ * @param {Model } model
2567
+ * @param {Schema } schema
2568
+ */
2569
+ var applyMethods = function ( model , schema ) {
2570
+ for ( var i in schema . methods ) {
2571
+ if ( typeof schema . methods [ i ] === 'function' ) {
2572
+ model . prototype [ i ] = schema . methods [ i ] ;
2573
+ } else {
2574
+ ( function ( _i ) {
2575
+ Object . defineProperty ( model . prototype , _i , {
2576
+ get : function ( ) {
2577
+ var h = { } ;
2578
+ for ( var k in schema . methods [ _i ] ) {
2579
+ h [ k ] = schema . methods [ _i ] [ k ] . bind ( this ) ;
2580
+ }
2581
+ return h ;
2582
+ }
2583
+ } ) ;
2584
+ } ) ( i ) ;
2585
+ }
2586
+ }
2587
+
2588
+ for ( var i in schema . methods )
2589
+ model . prototype [ i ] = schema . methods [ i ] ;
2590
+ } ;
2591
+
2592
+ /*!
2593
+ * Register statics for this model
2594
+ * @param {Model } model
2595
+ * @param {Schema } schema
2596
+ */
2597
+ var applyStatics = function ( model , schema ) {
2598
+ for ( var i in schema . statics ) {
2599
+ // use defineProperty so that static props can't be overwritten
2600
+ Object . defineProperty ( model , i , {
2601
+ value : schema . statics [ i ] ,
2602
+ writable : false
2603
+ } ) ;
2604
+ }
2605
+
2606
+ for ( var i in schema . statics )
2607
+ model [ i ] = schema . statics [ i ] ;
2608
+ } ;
2609
+
2563
2610
/*!
2564
2611
* Subclass this model with `conn`, `schema`, and `collection` settings.
2565
2612
*
0 commit comments