Browse Source

lifecycle hooks

dev
Evan You 9 years ago
parent
commit
0694a9bfb6
  1. 2
      src/runtime-with-compiler.js
  2. 28
      src/runtime/instance/index.js
  3. 39
      src/runtime/instance/lifecycle.js
  4. 5
      src/runtime/instance/render.js

2
src/runtime-with-compiler.js

@ -1,6 +1,6 @@
import config from './runtime/config'
import { compile } from './compiler/index'
import { getOuterHTML, extend, query, warn } from './runtime/util/index'
import { getOuterHTML, query, warn } from './runtime/util/index'
import Vue from './runtime/index'
const mount = Vue.prototype.$mount

28
src/runtime/instance/index.js

@ -1,36 +1,34 @@
import { initState, stateMixin } from './state'
import { initRender, renderMixin } from './render'
import { initEvents, eventsMixin } from './events'
import { initLifecycle, lifecycleMixin } from './lifecycle'
import { initLifecycle, lifecycleMixin, callHook } from './lifecycle'
import { nextTick, mergeOptions } from '../util/index'
let uid = 0
export default function Vue (options) {
options = options || {}
this.$parent = options.parent
this.$root = this.$parent
? this.$parent.$root
: this
this.$children = []
this.$refs = {} // child vm references
this.$els = {} // element references
// a uid
this._uid = uid++
// a flag to avoid this being observed
this._isVue = true
options = this.$options = mergeOptions(
this.$parent = options && options.parent
this.$root = this.$parent ? this.$parent.$root : this
this.$refs = {}
this.$els = {}
// merge options
this.$options = mergeOptions(
this.constructor.options,
options,
options || {},
this
)
initState(this)
initEvents(this)
initLifecycle(this)
initEvents(this)
callHook(this, 'init')
initState(this)
callHook(this, 'created')
initRender(this)
}

39
src/runtime/instance/lifecycle.js

@ -1,13 +1,40 @@
export function initLifecycle (vm) {
export function callHook (vm, hook) {
vm.$emit('pre-hook:' + hook)
var handlers = vm.$options[hook]
if (handlers) {
for (var i = 0, j = handlers.length; i < j; i++) {
handlers[i].call(vm)
}
}
vm.$emit('hook:' + hook)
}
export function initLifecycle (vm) {
vm._isDestroyed = false
vm._isBeingDestroyed = false
}
export function lifecycleMixin (Vue) {
Vue.prototype.$mount = function () {
}
Vue.prototype.$destroy = function () {
if (this._isDestroyed) {
return
}
this._callHook('beforeDestroy')
this._isBeingDestroyed = true
// teardown watchers
let i = this._watchers.length
while (i--) {
this._watchers[i].teardown()
}
// remove reference from data ob
// frozen object may not have observer.
if (this._data.__ob__) {
this._data.__ob__.removeVm(this)
}
// call the last hook...
this._isDestroyed = true
this._callHook('destroyed')
// turn off all instance listeners.
this.$off()
}
}

5
src/runtime/instance/render.js

@ -1,6 +1,7 @@
import Watcher from '../observer/watcher'
import { query } from '../util/index'
import { h, patch } from '../vdom/index'
import { callHook } from './lifecycle'
export function initRender (vm) {
const el = vm.$options.el
@ -13,19 +14,23 @@ export function renderMixin (Vue) {
Vue.prototype.__h__ = h
Vue.prototype._update = function (vtree) {
callHook(this, 'beforeUpdate')
if (!this._tree) {
patch(this.$el, vtree)
} else {
patch(this._tree, vtree)
}
this._tree = vtree
callHook(this, 'updated')
}
Vue.prototype.$mount = function (el) {
callHook(this, 'beforeMount')
this.$el = el ? query(el) : document.createElement('div')
this.$el.innerHTML = ''
this._watcher = new Watcher(this, this.$options.render, this._update)
this._update(this._watcher.value)
callHook(this, 'mounted')
}
Vue.prototype.$forceUpdate = function () {

Loading…
Cancel
Save