Skip to content

Commit

Permalink
warn unobservable objects
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Aug 30, 2015
1 parent d4d61ef commit 5705239
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/observer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function Observer (value) {
: copyAugment
augment(value, arrayMethods, arrayKeys)
this.observeArray(value)
} else if (_.isPlainObject(value)) {
} else {
this.walk(value)
}
}
Expand All @@ -52,11 +52,18 @@ Observer.create = function (value, vm) {
) {
ob = value.__ob__
} else if (
_.isObject(value) &&
(_.isArray(value) || _.isPlainObject(value)) &&
!Object.isFrozen(value) &&
!value._isVue
) {
ob = new Observer(value)
} else if (process.env.NODE_ENV !== 'production') {
if (_.isObject(value) && !_.isArray(value) && !_.isPlainObject(value)) {
_.warn(
'Unobservable object found in data: ' +
Object.prototype.toString.call(value)
)
}
}
if (ob && vm) {
ob.addVm(vm)
Expand Down
9 changes: 9 additions & 0 deletions test/unit/specs/observer/observer_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ var _ = require('../../../../src/util')

describe('Observer', function () {

beforeEach(function () {
spyOn(_, 'warn')
})

it('create on non-observables', function () {
// skip primitive value
var ob = Observer.create(1)
Expand Down Expand Up @@ -193,4 +197,9 @@ describe('Observer', function () {
config.proto = true
})

it('warn unobservable object', function () {
Observer.create(window)
expect(hasWarned(_, 'Unobservable object found in data')).toBe(true)
})

})

0 comments on commit 5705239

Please sign in to comment.