| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- var _ = require('../util')
- var compile = require('../compile/compile')
- /**
- * Set instance target element and kick off the compilation
- * process. The passed in `el` can be a selector string, an
- * existing Element, or a DocumentFragment (for block
- * instances).
- *
- * @param {Element|DocumentFragment|string} el
- * @public
- */
- exports.$mount = function (el) {
- if (this._isCompiled) {
- _.warn('$mount() should be called only once.')
- return
- }
- if (!el) {
- el = document.createElement('div')
- } else if (typeof el === 'string') {
- var selector = el
- el = document.querySelector(el)
- if (!el) {
- _.warn('Cannot find element: ' + selector)
- return
- }
- }
- this._compile(el)
- this._isCompiled = true
- this._callHook('compiled')
- if (_.inDoc(this.$el)) {
- this._callHook('attached')
- this._initDOMHooks()
- ready.call(this)
- } else {
- this._initDOMHooks()
- this.$once('hook:attached', ready)
- }
- return this
- }
- /**
- * Mark an instance as ready.
- */
- function ready () {
- this._isAttached = true
- this._isReady = true
- this._callHook('ready')
- }
- /**
- * Teardown an instance, unobserves the data, unbind all the
- * directives, turn off all the event listeners, etc.
- *
- * @param {Boolean} remove - whether to remove the DOM node.
- * @public
- */
- exports.$destroy = function (remove) {
- if (this._isDestroyed) {
- return
- }
- this._callHook('beforeDestroy')
- this._isBeingDestroyed = true
- var i
- // remove self from parent. only necessary
- // if parent is not being destroyed as well.
- var parent = this.$parent
- if (parent && !parent._isBeingDestroyed) {
- i = parent._children.indexOf(this)
- parent._children.splice(i, 1)
- }
- // destroy all children.
- if (this._children) {
- i = this._children.length
- while (i--) {
- this._children[i].$destroy()
- }
- }
- // teardown all directives. this also tearsdown all
- // directive-owned watchers.
- i = this._directives.length
- while (i--) {
- this._directives[i]._teardown()
- }
- // teardown all user watchers.
- for (i in this._userWatchers) {
- this._userWatchers[i].teardown()
- }
- // remove reference to self on $el
- if (this.$el) {
- this.$el.__vue__ = null
- }
- // remove DOM element
- var self = this
- if (remove && this.$el) {
- this.$remove(function () {
- cleanup(self)
- })
- } else {
- cleanup(self)
- }
- }
- /**
- * Clean up to ensure garbage collection.
- * This is called after the leave transition if there
- * is any.
- *
- * @param {Vue} vm
- */
- function cleanup (vm) {
- // remove reference from data ob
- vm._data.__ob__.removeVm(vm)
- vm._data =
- vm._watchers =
- vm._userWatchers =
- vm._watcherList =
- vm.$el =
- vm.$parent =
- vm.$root =
- vm._children =
- vm._bindings =
- vm._directives = null
- // call the last hook...
- vm._isDestroyed = true
- vm._callHook('destroyed')
- // turn off all instance listeners.
- vm.$off()
- }
- /**
- * Partially compile a piece of DOM and return a
- * decompile function.
- *
- * @param {Element|DocumentFragment} el
- * @return {Function}
- */
- exports.$compile = function (el) {
- return compile(el, this.$options, true)(this, el)
- }
|