Skip to content

Commit 5705239

Browse files
committed
warn unobservable objects
1 parent d4d61ef commit 5705239

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/observer/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function Observer (value) {
2525
: copyAugment
2626
augment(value, arrayMethods, arrayKeys)
2727
this.observeArray(value)
28-
} else if (_.isPlainObject(value)) {
28+
} else {
2929
this.walk(value)
3030
}
3131
}
@@ -52,11 +52,18 @@ Observer.create = function (value, vm) {
5252
) {
5353
ob = value.__ob__
5454
} else if (
55-
_.isObject(value) &&
55+
(_.isArray(value) || _.isPlainObject(value)) &&
5656
!Object.isFrozen(value) &&
5757
!value._isVue
5858
) {
5959
ob = new Observer(value)
60+
} else if (process.env.NODE_ENV !== 'production') {
61+
if (_.isObject(value) && !_.isArray(value) && !_.isPlainObject(value)) {
62+
_.warn(
63+
'Unobservable object found in data: ' +
64+
Object.prototype.toString.call(value)
65+
)
66+
}
6067
}
6168
if (ob && vm) {
6269
ob.addVm(vm)

test/unit/specs/observer/observer_spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ var _ = require('../../../../src/util')
55

66
describe('Observer', function () {
77

8+
beforeEach(function () {
9+
spyOn(_, 'warn')
10+
})
11+
812
it('create on non-observables', function () {
913
// skip primitive value
1014
var ob = Observer.create(1)
@@ -193,4 +197,9 @@ describe('Observer', function () {
193197
config.proto = true
194198
})
195199

200+
it('warn unobservable object', function () {
201+
Observer.create(window)
202+
expect(hasWarned(_, 'Unobservable object found in data')).toBe(true)
203+
})
204+
196205
})

0 commit comments

Comments
 (0)