-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprop.js
85 lines (76 loc) · 2.2 KB
/
prop.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
// NOTE: the prop internal directive is compiled and linked
// during _initScope(), before the created hook is called.
// The purpose is to make the initial prop values available
// inside `created` hooks and `data` functions.
var _ = require('../util')
var Watcher = require('../watcher')
var bindingModes = require('../config')._propBindingModes
module.exports = {
bind: function () {
var child = this.vm
var parent = child._context
// passed in from compiler directly
var prop = this._descriptor
var childKey = prop.path
var parentKey = prop.parentPath
// simple lock to avoid circular updates.
// without this it would stabilize too, but this makes
// sure it doesn't cause other watchers to re-evaluate.
var locked = false
function withLock (fn) {
return function (val) {
if (!locked) {
locked = true
fn(val)
_.nextTick(function () {
locked = false
})
}
}
}
this.parentWatcher = new Watcher(
parent,
parentKey,
withLock(function (val) {
if (_.assertProp(prop, val)) {
child[childKey] = val
}
})
)
// set the child initial value.
// !!! We need to set it also on raw data here, because
// props are initialized before data is fully observed
var value = this.parentWatcher.value
if (_.assertProp(prop, value)) {
if (childKey === '$data') {
child._data = value
} else {
child[childKey] = child._data[childKey] = value
}
}
// only setup two-way binding if this is not a one-way
// binding.
if (prop.mode === bindingModes.TWO_WAY) {
// important: defer the child watcher creation until
// the created hook (after data observation)
var self = this
child.$once('hook:created', function () {
self.childWatcher = new Watcher(
child,
childKey,
withLock(function (val) {
parent.$set(parentKey, val)
})
)
})
}
},
unbind: function () {
if (this.parentWatcher) {
this.parentWatcher.teardown()
}
if (this.childWatcher) {
this.childWatcher.teardown()
}
}
}