Skip to content

Commit

Permalink
execHook
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 18, 2013
1 parent 5473001 commit 48e68c1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
37 changes: 17 additions & 20 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,8 @@ function Compiler (vm, options) {
// setup observer
compiler.setupObserver()

// pre compile / created hook
var created = options.beforeCompile || options.created
if (created) {
created.call(vm, options)
}
// beforeCompile hook
compiler.execHook('beforeCompile', 'created')

// create bindings for things already in scope
var key, keyPrefix
Expand Down Expand Up @@ -125,10 +122,7 @@ function Compiler (vm, options) {
compiler.init = false

// post compile / ready hook
var ready = options.afterCompile || options.ready
if (ready) {
ready.call(vm, options)
}
compiler.execHook('afterCompile', 'ready')
}

var CompilerProto = Compiler.prototype
Expand Down Expand Up @@ -549,6 +543,17 @@ CompilerProto.getOption = function (type, id) {
return (opts[type] && opts[type][id]) || (utils[type] && utils[type][id])
}

/**
* Execute a user hook
*/
CompilerProto.execHook = function (id, alt) {
var opts = this.options,
hook = opts[id] || opts[alt]
if (hook) {
hook.call(this.vm, opts)
}
}

/**
* Unbind and remove element
*/
Expand All @@ -560,14 +565,9 @@ CompilerProto.destroy = function () {
el = compiler.el,
directives = compiler.dirs,
exps = compiler.exps,
bindings = compiler.bindings,
beforeDestroy = compiler.options.beforeDestroy,
afterDestroy = compiler.options.afterDestroy
bindings = compiler.bindings

// call user teardown first
if (beforeDestroy) {
beforeDestroy.call(vm)
}
compiler.execHook('beforeDestroy')

// unwatch
compiler.observer.off()
Expand Down Expand Up @@ -621,10 +621,7 @@ CompilerProto.destroy = function () {
vm.$remove()
}

// post teardown hook
if (afterDestroy) {
afterDestroy.call(vm)
}
compiler.execHook('afterDestroy')
}

// Helpers --------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions test/unit/specs/viewmodel.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@ describe('UNIT: ViewModel', function () {
$remove: function () {
elRemoved = true
}
},
execHook: function (id) {
this.options[id].call(this)
}
}

Expand Down

0 comments on commit 48e68c1

Please sign in to comment.