Răsfoiți Sursa

tests for new partial

Evan You 11 ani în urmă
părinte
comite
2a78b90c71

+ 8 - 4
src/compiler/transclude.js

@@ -63,14 +63,18 @@ function transcludeTemplate (el, options) {
     _.warn('Invalid template option: ' + template)
   } else {
     var replacer = frag.firstChild
+    var tag = replacer.tagName && replacer.tagName.toLowerCase()
     if (options.replace) {
       if (
+        // multi-children template
         frag.childNodes.length > 1 ||
+        // non-element template
         replacer.nodeType !== 1 ||
-        // when root node is <content> or has v-repeat, the
-        // instance could end up having multiple top-level
-        // nodes, thus becoming a block instance.
-        replacer.tagName.toLowerCase() === 'content' ||
+        // when root node is <content>, <partial> or has
+        // v-repeat, the instance could end up having
+        // multiple top-level nodes, thus becoming a block
+        // instance.
+        tag === 'content' || tag === 'partial' ||
         replacer.hasAttribute(config.prefix + 'repeat')
       ) {
         return frag

+ 1 - 1
src/directives/repeat.js

@@ -358,7 +358,7 @@ module.exports = {
       // is this a component?
       _asComponent: this.asComponent,
       // linker cachable if no inline-template
-      _linkerCachable: !this.inlineTemplate,
+      _linkerCachable: !this.inlineTemplate && Ctor !== _.Vue,
       // transclusion host
       _host: this._host,
       // pre-compiled linker for simple repeats

+ 98 - 0
test/unit/specs/element-directives/partial_spec.js

@@ -0,0 +1,98 @@
+var Vue = require('../../../../src/vue')
+var _ = require('../../../../src/util')
+var compiler = require('../../../../src/compiler')
+
+describe('Partial', function () {
+
+  var el
+  beforeEach(function () {
+    el = document.createElement('div')
+  })
+
+  it('static', function (done) {
+    var vm = new Vue({
+      el: el,
+      template: '<partial name="yo"></partial>',
+      data: {
+        msg: 'hello'
+      },
+      partials: {
+        yo: '{{msg}}'
+      }
+    })
+    expect(el.textContent).toBe('hello')
+    vm.msg = 'what'
+    _.nextTick(function () {
+      expect(el.textContent).toBe('what')
+      done()
+    })
+  })
+
+  it('dynamic', function (done) {
+    var vm = new Vue({
+      el: el,
+      template: '<partial name="test-{{id}}"></partial>',
+      data: {
+        id: 'a',
+      },
+      partials: {
+        'test-a': 'a {{id}}',
+        'test-b': 'b {{id}}'
+      }
+    })
+    expect(el.textContent).toBe('a a')
+    vm.id = 'b'
+    _.nextTick(function () {
+      expect(el.textContent).toBe('b b')
+      done()
+    })
+  })
+
+  it('caching', function () {
+    var calls = 0
+    var compile = compiler.compile
+    compiler.compile = function () {
+      calls++
+      return compile.apply(this, arguments)
+    }
+    var vm = new Vue({
+      el: el,
+      template:
+        '<partial name="cache-test"></partial> ' +
+        '<partial name="cache-test"></partial>',
+      data: {
+        msg: 'hi'
+      },
+      partials: {
+        'cache-test': 'cache-test {{msg}}'
+      }
+    })
+    expect(el.textContent).toBe('cache-test hi cache-test hi')
+    // one call for instance, and one for partial
+    expect(calls).toBe(2)
+    // cleanup
+    compiler.compile = compile
+  })
+
+  it('teardown', function () {
+    var vm = new Vue({
+      el: el,
+      template: '<partial name="test-{{id}}"></partial>',
+      data: {
+        id: 'a',
+      },
+      partials: {
+        'test-a': 'a {{id}}'
+      }
+    })
+    expect(vm._directives.length).toBe(2)
+    expect(vm._directives[1].name).toBe('partial')
+    expect(vm._watchers.length).toBe(2)
+    vm._directives[1]._teardown()
+    // the text-directive should've been removed.
+    expect(vm._directives.length).toBe(1)
+    expect(vm._directives[0].name).toBe('partial')
+    expect(vm._watchers.length).toBe(0)
+  })
+
+})