-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprop.js
75 lines (65 loc) · 1.66 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
var _ = require('../util')
var Watcher = require('../watcher')
var identRE = require('../parsers/path').identRE
module.exports = {
bind: function () {
var child = this.vm
var parent = child.$parent
var childKey = this.arg
var parentKey = this.expression
if (!identRE.test(childKey)) {
_.warn(
'Invalid prop key: "' + childKey + '". Prop keys ' +
'must be valid identifiers.'
)
}
// 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
var lock = function () {
locked = true
_.nextTick(unlock)
}
var unlock = function () {
locked = false
}
this.parentWatcher = new Watcher(
parent,
parentKey,
function (val) {
if (!locked) {
lock()
// all props have been initialized already
child[childKey] = val
}
}
)
// set the child initial value first, before setting
// up the child watcher to avoid triggering it
// immediately.
child.$set(childKey, this.parentWatcher.value)
// only setup two-way binding if this is not a one-way
// binding.
if (!this._descriptor.oneWay) {
this.childWatcher = new Watcher(
child,
childKey,
function (val) {
if (!locked) {
lock()
parent.$set(parentKey, val)
}
}
)
}
},
unbind: function () {
if (this.parentWatcher) {
this.parentWatcher.teardown()
}
if (this.childWatcher) {
this.childWatcher.teardown()
}
}
}