Skip to content

Commit 48e68c1

Browse files
committed
execHook
1 parent 5473001 commit 48e68c1

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

src/compiler.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

134128
var 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 --------------------------------------------------------------------

test/unit/specs/viewmodel.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ describe('UNIT: ViewModel', function () {
306306
$remove: function () {
307307
elRemoved = true
308308
}
309+
},
310+
execHook: function (id) {
311+
this.options[id].call(this)
309312
}
310313
}
311314

0 commit comments

Comments
 (0)