Просмотр исходного кода

should not observer other Vue instances

Evan You 12 лет назад
Родитель
Сommit
28a2c68ad4
2 измененных файлов с 13 добавлено и 2 удалено
  1. 6 2
      src/observer.js
  2. 7 0
      test/unit/specs/observer.js

+ 6 - 2
src/observer.js

@@ -16,7 +16,10 @@ var Emitter  = require('./emitter'),
     // define methods as inenumerable if __proto__ is present,
     // otherwise enumerable so we can loop through and manually
     // attach to array instances
-    hasProto = ({}).__proto__
+    hasProto = ({}).__proto__,
+
+    // lazy load
+    ViewModel
 
 // The proxy prototype to replace the __proto__ of
 // an observed array
@@ -161,8 +164,9 @@ function bind (obj, key, path, observer) {
  *  Check if a value is watchable
  */
 function isWatchable (obj) {
+    ViewModel = ViewModel || require('./viewmodel')
     var type = typeOf(obj)
-    return type === 'Object' || type === 'Array'
+    return (type === 'Object' || type === 'Array') && !(obj instanceof ViewModel)
 }
 
 /**

+ 7 - 0
test/unit/specs/observer.js

@@ -5,6 +5,13 @@ describe('UNIT: Observer', function () {
         DepsOb   = require('vue/src/deps-parser').observer
     
     describe('Observing Object', function () {
+
+        it('should not watch a ViewModel instance', function () {
+            var obj = new Vue(), ob = new Emitter()
+            ob.proxies = {}
+            Observer.observe(obj, 'test', ob)
+            assert.notOk(obj.__observer__)
+        })
         
         it('should attach hidden observer and values to the object', function () {
             var obj = {}, ob = new Emitter()