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

allow nested path for computed properties that return objects

Evan You 12 лет назад
Родитель
Сommit
bedcb22f9c
2 измененных файлов с 29 добавлено и 0 удалено
  1. 3 0
      src/compiler.js
  2. 26 0
      test/unit/specs/misc.js

+ 3 - 0
src/compiler.js

@@ -608,6 +608,9 @@ CompilerProto.createBinding = function (key, isExp, isFn) {
             } else {
                 compiler.defineMeta(key, binding)
             }
+        } else if (computed && computed[key.split('.')[0]]) {
+            // nested path on computed property
+            compiler.defineExp(key, binding)
         } else {
             // ensure path in data so it can be observed
             Observer.ensurePath(compiler.data, key)

+ 26 - 0
test/unit/specs/misc.js

@@ -49,9 +49,11 @@ describe('Misc Features', function () {
     })
 
     describe('computed properties', function () {
+
         it('should be accessible like a normal attribtue', function () {
             var b = 2
             var v = new Vue({
+                template: '{{nested.value.a}}',
                 data: {
                     a: 1,
                 },
@@ -80,6 +82,30 @@ describe('Misc Features', function () {
             v.test = 10
             assert.strictEqual(b, 8)
         })
+
+        it('should be bindable in templates, even nested', function (done) {
+            var v = new Vue({
+                template: '{{nested.value.a}}',
+                data: { a: 1 },
+                computed: {
+                    nested: function () {
+                        return {
+                            value: {
+                                a: this.a + 2
+                            }
+                        }
+                    }
+                }
+            })
+            assert.strictEqual(v.$el.textContent, '3')
+
+            v.a = 2
+            nextTick(function () {
+                assert.strictEqual(v.$el.textContent, '4')
+                done()
+            })
+        })
+
     })
 
     describe('setting an object to empty', function () {