forked from vuejs/vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.js
241 lines (220 loc) · 5.2 KB
/
state.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
var _ = require('../util')
var compiler = require('../compiler')
var Observer = require('../observer')
var Dep = require('../observer/dep')
var Watcher = require('../watcher')
/**
* Setup the scope of an instance, which contains:
* - observed data
* - computed properties
* - user methods
* - meta properties
*/
exports._initState = function () {
this._initProps()
this._initMeta()
this._initMethods()
this._initData()
this._initComputed()
}
/**
* Initialize props.
*/
exports._initProps = function () {
var options = this.$options
var el = options.el
var props = options.props
if (props && !el) {
process.env.NODE_ENV !== 'production' && _.warn(
'Props will not be compiled if no `el` option is ' +
'provided at instantiation.'
)
}
// make sure to convert string selectors into element now
el = options.el = _.query(el)
this._propsUnlinkFn = el && el.nodeType === 1 && props
// props must be linked in proper scope if inside v-for
? compiler.compileAndLinkProps(this, el, props, this._scope)
: null
}
/**
* Initialize the data.
*/
exports._initData = function () {
var propsData = this._data
var optionsDataFn = this.$options.data
var optionsData = optionsDataFn && optionsDataFn()
if (optionsData) {
this._data = optionsData
for (var prop in propsData) {
if (process.env.NODE_ENV !== 'production' &&
optionsData.hasOwnProperty(prop)) {
_.warn(
'Data field "' + prop + '" is already defined ' +
'as a prop. Use prop default value instead.'
)
}
if (this._props[prop].raw !== null ||
!optionsData.hasOwnProperty(prop)) {
_.set(optionsData, prop, propsData[prop])
}
}
}
var data = this._data
// proxy data on instance
var keys = Object.keys(data)
var i, key
i = keys.length
while (i--) {
key = keys[i]
this._proxy(key)
}
// observe data
Observer.create(data, this)
}
/**
* Swap the isntance's $data. Called in $data's setter.
*
* @param {Object} newData
*/
exports._setData = function (newData) {
newData = newData || {}
var oldData = this._data
this._data = newData
var keys, key, i
// unproxy keys not present in new data
keys = Object.keys(oldData)
i = keys.length
while (i--) {
key = keys[i]
if (!(key in newData)) {
this._unproxy(key)
}
}
// proxy keys not already proxied,
// and trigger change for changed values
keys = Object.keys(newData)
i = keys.length
while (i--) {
key = keys[i]
if (!this.hasOwnProperty(key)) {
// new property
this._proxy(key)
}
}
oldData.__ob__.removeVm(this)
Observer.create(newData, this)
this._digest()
}
/**
* Proxy a property, so that
* vm.prop === vm._data.prop
*
* @param {String} key
*/
exports._proxy = function (key) {
if (!_.isReserved(key)) {
// need to store ref to self here
// because these getter/setters might
// be called by child scopes via
// prototype inheritance.
var self = this
Object.defineProperty(self, key, {
configurable: true,
enumerable: true,
get: function proxyGetter () {
return self._data[key]
},
set: function proxySetter (val) {
self._data[key] = val
}
})
}
}
/**
* Unproxy a property.
*
* @param {String} key
*/
exports._unproxy = function (key) {
if (!_.isReserved(key)) {
delete this[key]
}
}
/**
* Force update on every watcher in scope.
*/
exports._digest = function () {
for (var i = 0, l = this._watchers.length; i < l; i++) {
this._watchers[i].update(true) // shallow updates
}
}
/**
* Setup computed properties. They are essentially
* special getter/setters
*/
function noop () {}
exports._initComputed = function () {
var computed = this.$options.computed
if (computed) {
for (var key in computed) {
var userDef = computed[key]
var def = {
enumerable: true,
configurable: true
}
if (typeof userDef === 'function') {
def.get = makeComputedGetter(userDef, this)
def.set = noop
} else {
def.get = userDef.get
? userDef.cache !== false
? makeComputedGetter(userDef.get, this)
: _.bind(userDef.get, this)
: noop
def.set = userDef.set
? _.bind(userDef.set, this)
: noop
}
Object.defineProperty(this, key, def)
}
}
}
function makeComputedGetter (getter, owner) {
var watcher = new Watcher(owner, getter, null, {
lazy: true
})
return function computedGetter () {
if (watcher.dirty) {
watcher.evaluate()
}
if (Dep.target) {
watcher.depend()
}
return watcher.value
}
}
/**
* Setup instance methods. Methods must be bound to the
* instance since they might be passed down as a prop to
* child components.
*/
exports._initMethods = function () {
var methods = this.$options.methods
if (methods) {
for (var key in methods) {
this[key] = _.bind(methods[key], this)
}
}
}
/**
* Initialize meta information like $index, $key & $value.
*/
exports._initMeta = function () {
var metas = this.$options._meta
if (metas) {
for (var key in metas) {
_.defineReactive(this, key, metas[key])
}
}
}