@@ -44,9 +44,11 @@ function Observer (value) {
44
44
*/
45
45
46
46
Observer . create = function ( value , vm ) {
47
+ if ( ! value || typeof value !== 'object' ) {
48
+ return
49
+ }
47
50
var ob
48
51
if (
49
- value &&
50
52
value . hasOwnProperty ( '__ob__' ) &&
51
53
value . __ob__ instanceof Observer
52
54
) {
@@ -82,18 +84,6 @@ Observer.prototype.walk = function (obj) {
82
84
}
83
85
}
84
86
85
- /**
86
- * Try to carete an observer for a child value,
87
- * and if value is array, link dep to the array.
88
- *
89
- * @param {* } val
90
- * @return {Dep|undefined }
91
- */
92
-
93
- Observer . prototype . observe = function ( val ) {
94
- return Observer . create ( val )
95
- }
96
-
97
87
/**
98
88
* Observe a list of Array items.
99
89
*
@@ -103,7 +93,7 @@ Observer.prototype.observe = function (val) {
103
93
Observer . prototype . observeArray = function ( items ) {
104
94
var i = items . length
105
95
while ( i -- ) {
106
- var ob = this . observe ( items [ i ] )
96
+ var ob = Observer . create ( items [ i ] )
107
97
if ( ob ) {
108
98
( ob . parents || ( ob . parents = [ ] ) ) . push ( this )
109
99
}
@@ -151,28 +141,7 @@ Observer.prototype.notify = function () {
151
141
*/
152
142
153
143
Observer . prototype . convert = function ( key , val ) {
154
- var ob = this
155
- var childOb = ob . observe ( val )
156
- var dep = new Dep ( )
157
- Object . defineProperty ( ob . value , key , {
158
- enumerable : true ,
159
- configurable : true ,
160
- get : function ( ) {
161
- if ( Dep . target ) {
162
- dep . depend ( )
163
- if ( childOb ) {
164
- childOb . dep . depend ( )
165
- }
166
- }
167
- return val
168
- } ,
169
- set : function ( newVal ) {
170
- if ( newVal === val ) return
171
- val = newVal
172
- childOb = ob . observe ( newVal )
173
- dep . notify ( )
174
- }
175
- } )
144
+ defineReactive ( this . value , key , val )
176
145
}
177
146
178
147
/**
@@ -230,4 +199,39 @@ function copyAugment (target, src, keys) {
230
199
}
231
200
}
232
201
202
+ /**
203
+ * Define a reactive property on an Object.
204
+ *
205
+ * @param {Object } obj
206
+ * @param {String } key
207
+ * @param {* } val
208
+ */
209
+
210
+ function defineReactive ( obj , key , val ) {
211
+ var dep = new Dep ( )
212
+ var childOb = Observer . create ( val )
213
+ Object . defineProperty ( obj , key , {
214
+ enumerable : true ,
215
+ configurable : true ,
216
+ get : function metaGetter ( ) {
217
+ if ( Dep . target ) {
218
+ dep . depend ( )
219
+ if ( childOb ) {
220
+ childOb . dep . depend ( )
221
+ }
222
+ }
223
+ return val
224
+ } ,
225
+ set : function metaSetter ( newVal ) {
226
+ if ( newVal === val ) return
227
+ val = newVal
228
+ childOb = Observer . create ( newVal )
229
+ dep . notify ( )
230
+ }
231
+ } )
232
+ }
233
+
234
+ // Attach to the util object so it can be used elsewhere.
235
+ _ . defineReactive = defineReactive
236
+
233
237
module . exports = Observer
0 commit comments