| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- var _ = require('../util')
- var inDoc = _.inDoc
- /**
- * Setup the instance's option events & watchers.
- * If the value is a string, we pull it from the
- * instance's methods by name.
- */
- exports._initEvents = function () {
- var options = this.$options
- registerCallbacks(this, '$on', options.events)
- registerCallbacks(this, '$watch', options.watch)
- }
- /**
- * Register callbacks for option events and watchers.
- *
- * @param {Vue} vm
- * @param {String} action
- * @param {Object} hash
- */
- function registerCallbacks (vm, action, hash) {
- if (!hash) return
- var handlers, key, i, j
- for (key in hash) {
- handlers = hash[key]
- if (_.isArray(handlers)) {
- for (i = 0, j = handlers.length; i < j; i++) {
- register(vm, action, key, handlers[i])
- }
- } else {
- register(vm, action, key, handlers)
- }
- }
- }
- /**
- * Helper to register an event/watch callback.
- *
- * @param {Vue} vm
- * @param {String} action
- * @param {String} key
- * @param {Function|String|Object} handler
- * @param {Object} [options]
- */
- function register (vm, action, key, handler, options) {
- var type = typeof handler
- if (type === 'function') {
- vm[action](key, handler, options)
- } else if (type === 'string') {
- var methods = vm.$options.methods
- var method = methods && methods[handler]
- if (method) {
- vm[action](key, method, options)
- } else {
- _.warn(
- 'Unknown method: "' + handler + '" when ' +
- 'registering callback for ' + action +
- ': "' + key + '".'
- )
- }
- } else if (handler && type === 'object') {
- register(vm, action, key, handler.handler, handler)
- }
- }
- /**
- * Setup recursive attached/detached calls
- */
- exports._initDOMHooks = function () {
- this.$on('hook:attached', onAttached)
- this.$on('hook:detached', onDetached)
- }
- /**
- * Callback to recursively call attached hook on children
- */
- function onAttached () {
- if (!this._isAttached) {
- this._isAttached = true
- this.$children.forEach(callAttach)
- }
- }
- /**
- * Iterator to call attached hook
- *
- * @param {Vue} child
- */
- function callAttach (child) {
- if (!child._isAttached && inDoc(child.$el)) {
- child._callHook('attached')
- }
- }
- /**
- * Callback to recursively call detached hook on children
- */
- function onDetached () {
- if (this._isAttached) {
- this._isAttached = false
- this.$children.forEach(callDetach)
- }
- }
- /**
- * Iterator to call detached hook
- *
- * @param {Vue} child
- */
- function callDetach (child) {
- if (child._isAttached && !inDoc(child.$el)) {
- child._callHook('detached')
- }
- }
- /**
- * Trigger all handlers for a hook
- *
- * @param {String} hook
- */
- exports._callHook = function (hook) {
- var handlers = this.$options[hook]
- if (handlers) {
- for (var i = 0, j = handlers.length; i < j; i++) {
- handlers[i].call(this)
- }
- }
- this.$emit('hook:' + hook)
- }
|