Evan You пре 13 година
родитељ
комит
ea792ba6bb
3 измењених фајлова са 25 додато и 17 уклоњено
  1. 17 14
      examples/nested-props.html
  2. 5 2
      src/compiler.js
  3. 3 1
      src/observe.js

+ 17 - 14
examples/nested-props.html

@@ -14,23 +14,26 @@
         <button sd-on="click:three">three</button>
         <script>
             Seed.config({debug: true})
+            var data1 = {
+                c: 0,
+                b: {
+                    c: 'zero'
+                }
+            },
+            data = {
+                c: 1,
+                b: {
+                    c: 'one'
+                }
+            }
             var Demo = Seed.ViewModel.extend({
                 init: function () {
-                    this.a = {
-                        c: 0,
-                        b: {
-                            c: 'zero'
-                        }
-                    }  
+                    this.msg = 'Yoyoyo'
+                    this.a = data1
                 },
                 props: {
                     one: function () {
-                        this.a = {
-                            c: 1,
-                            b: {
-                                c: 'one'
-                            }
-                        }
+                        this.a = data
                     },
                     two: function () {
                         this.a.b = {
@@ -42,8 +45,8 @@
                         this.a.b.c = 'three'
                         this.a.c = 3
                     },
-                    d: {get: function () {
-                        return (this.a.b.c + this.a.c) || ''
+                    d: {get: function (ctx) {
+                        return (ctx.vm.msg + this.a.b.c + this.a.c) || ''
                     }}
                 }
             })

+ 5 - 2
src/compiler.js

@@ -100,7 +100,6 @@ CompilerProto.setupObserver = function () {
             }
         })
         .on('set', function (key, val) {
-            console.log('set:', key, '=>', val)
             if (!bindings[key]) compiler.createBinding(key)
             bindings[key].update(val)
         })
@@ -253,7 +252,11 @@ CompilerProto.define = function (key, binding) {
     Object.defineProperty(this.vm, key, {
         enumerable: true,
         get: function () {
-            compiler.observer.emit('get', key)
+            if (!binding.isComputed && !binding.value.__observer__) {
+                // only emit non-computed, non-observed values
+                // because these are the cleanest dependencies
+                compiler.observer.emit('get', key)
+            }
             return binding.isComputed
                 ? binding.value.get({
                     el: compiler.el,

+ 3 - 1
src/observe.js

@@ -95,6 +95,7 @@ function watchArray (arr, path, observer) {
 
 function bind (obj, key, path, observer) {
     var val = obj[key],
+        watchable = isWatchable(val),
         values = obj.__values__,
         fullKey = (path ? path + '.' : '') + key
     values[fullKey] = val
@@ -102,7 +103,8 @@ function bind (obj, key, path, observer) {
     def(obj, key, {
         enumerable: true,
         get: function () {
-            observer.emit('get', fullKey)
+            // only emit get on tip values
+            if (!watchable) observer.emit('get', fullKey)
             return values[fullKey]
         },
         set: function (newVal) {