Skip to content

Commit 7b8d20f

Browse files
committed
observer: defineReactive should track object dep too (fix #1370)
1 parent 9ebeaa3 commit 7b8d20f

File tree

2 files changed

+40
-64
lines changed

2 files changed

+40
-64
lines changed

src/observer/index.js

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ function Observer (value) {
4444
*/
4545

4646
Observer.create = function (value, vm) {
47+
if (!value || typeof value !== 'object') {
48+
return
49+
}
4750
var ob
4851
if (
49-
value &&
5052
value.hasOwnProperty('__ob__') &&
5153
value.__ob__ instanceof Observer
5254
) {
@@ -82,18 +84,6 @@ Observer.prototype.walk = function (obj) {
8284
}
8385
}
8486

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-
9787
/**
9888
* Observe a list of Array items.
9989
*
@@ -103,7 +93,7 @@ Observer.prototype.observe = function (val) {
10393
Observer.prototype.observeArray = function (items) {
10494
var i = items.length
10595
while (i--) {
106-
var ob = this.observe(items[i])
96+
var ob = Observer.create(items[i])
10797
if (ob) {
10898
(ob.parents || (ob.parents = [])).push(this)
10999
}
@@ -151,28 +141,7 @@ Observer.prototype.notify = function () {
151141
*/
152142

153143
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)
176145
}
177146

178147
/**
@@ -230,4 +199,39 @@ function copyAugment (target, src, keys) {
230199
}
231200
}
232201

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+
233237
module.exports = Observer

src/util/lang.js

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
var Dep = require('../observer/dep')
2-
31
/**
42
* Check if an expression is a literal value.
53
*
@@ -237,32 +235,6 @@ exports.define = function (obj, key, val, enumerable) {
237235
})
238236
}
239237

240-
/**
241-
* Define a reactive property.
242-
*
243-
* @param {Object} obj
244-
* @param {String} key
245-
* @param {*} val
246-
*/
247-
248-
exports.defineReactive = function (obj, key, val) {
249-
var dep = new Dep()
250-
Object.defineProperty(obj, key, {
251-
get: function metaGetter () {
252-
if (Dep.target) {
253-
dep.depend()
254-
}
255-
return val
256-
},
257-
set: function metaSetter (newVal) {
258-
if (val !== newVal) {
259-
val = newVal
260-
dep.notify()
261-
}
262-
}
263-
})
264-
}
265-
266238
/**
267239
* Debounce a function so it only gets called after the
268240
* input stops arriving after the given wait period.

0 commit comments

Comments
 (0)