-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.js
122 lines (108 loc) · 2.56 KB
/
events.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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 {*} handler
*/
function register (vm, action, key, handler) {
var type = typeof handler
if (type === 'function') {
vm[action](key, handler)
} else if (type === 'string') {
var methods = vm.$options.methods
var method = methods && methods[handler]
if (method) {
vm[action](key, method)
} else {
_.warn(
'Unknown method: "' + handler + '" when ' +
'registering callback for ' + action +
': "' + key + '".'
)
}
}
}
/**
* 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 () {
this._isAttached = true
var children = this._children
if (!children) return
for (var i = 0, l = children.length; i < l; i++) {
var child = children[i]
if (!child._isAttached && inDoc(child.$el)) {
child._callHook('attached')
}
}
}
/**
* Callback to recursively call detached hook on children
*/
function onDetached () {
this._isAttached = false
var children = this._children
if (!children) return
for (var i = 0, l = children.length; i < l; i++) {
var child = children[i]
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)
}