@@ -83,11 +83,8 @@ function Compiler (vm, options) {
8383 // setup observer
8484 compiler . setupObserver ( )
8585
86- // pre compile / created hook
87- var created = options . beforeCompile || options . created
88- if ( created ) {
89- created . call ( vm , options )
90- }
86+ // beforeCompile hook
87+ compiler . execHook ( 'beforeCompile' , 'created' )
9188
9289 // create bindings for things already in scope
9390 var key , keyPrefix
@@ -125,10 +122,7 @@ function Compiler (vm, options) {
125122 compiler . init = false
126123
127124 // post compile / ready hook
128- var ready = options . afterCompile || options . ready
129- if ( ready ) {
130- ready . call ( vm , options )
131- }
125+ compiler . execHook ( 'afterCompile' , 'ready' )
132126}
133127
134128var CompilerProto = Compiler . prototype
@@ -549,6 +543,17 @@ CompilerProto.getOption = function (type, id) {
549543 return ( opts [ type ] && opts [ type ] [ id ] ) || ( utils [ type ] && utils [ type ] [ id ] )
550544}
551545
546+ /**
547+ * Execute a user hook
548+ */
549+ CompilerProto . execHook = function ( id , alt ) {
550+ var opts = this . options ,
551+ hook = opts [ id ] || opts [ alt ]
552+ if ( hook ) {
553+ hook . call ( this . vm , opts )
554+ }
555+ }
556+
552557/**
553558 * Unbind and remove element
554559 */
@@ -560,14 +565,9 @@ CompilerProto.destroy = function () {
560565 el = compiler . el ,
561566 directives = compiler . dirs ,
562567 exps = compiler . exps ,
563- bindings = compiler . bindings ,
564- beforeDestroy = compiler . options . beforeDestroy ,
565- afterDestroy = compiler . options . afterDestroy
568+ bindings = compiler . bindings
566569
567- // call user teardown first
568- if ( beforeDestroy ) {
569- beforeDestroy . call ( vm )
570- }
570+ compiler . execHook ( 'beforeDestroy' )
571571
572572 // unwatch
573573 compiler . observer . off ( )
@@ -621,10 +621,7 @@ CompilerProto.destroy = function () {
621621 vm . $remove ( )
622622 }
623623
624- // post teardown hook
625- if ( afterDestroy ) {
626- afterDestroy . call ( vm )
627- }
624+ compiler . execHook ( 'afterDestroy' )
628625}
629626
630627// Helpers --------------------------------------------------------------------
0 commit comments