Ver Fonte

tests for v-partial

Evan You há 11 anos atrás
pai
commit
d2cba9a25c

+ 3 - 2
src/compile/compile.js

@@ -14,11 +14,12 @@ function noop () {}
  *
  * @param {Element|DocumentFragment} el
  * @param {Object} options
+ * @param {Boolean} partial
  * @return {Function}
  */
 
-module.exports = function compile (el, options) {
-  var params = options.paramAttributes
+module.exports = function compile (el, options, partial) {
+  var params = !partial && options.paramAttributes
   var paramsLinkFn = params
     ? compileParamAttributes(el, params, options)
     : null

+ 4 - 9
src/directives/partial.js

@@ -1,4 +1,5 @@
 var _ = require('../util')
+var compile = require('../compile/compile')
 var templateParser = require('../parse/template')
 
 module.exports = {
@@ -15,21 +16,15 @@ module.exports = {
     partial = templateParser.parse(partial, true)
     var el = this.el
     var vm = this.vm
-    // comment ref node means inline partial
     if (el.nodeType === 8) {
-      // keep a ref for the partial's content nodes
-      var nodes = _.toArray(partial.childNodes)
+      // comment ref node means inline partial
+      compile(partial, vm.$options)(vm, partial)
       _.replace(el, partial)
-      // compile partial after appending, because its
-      // children's parentNode will change from the fragment
-      // to the correct parentNode. This could affect
-      // directives that need access to its element's
-      // parentNode.
-      nodes.forEach(vm._compileNode, vm)
     } else {
       // just set innerHTML...
       el.innerHTML = ''
       el.appendChild(partial)
+      compile(el, vm.$options, true)(vm, el)
     }
   }
 

+ 52 - 0
test/unit/specs/directives/partial_spec.js

@@ -0,0 +1,52 @@
+var _ = require('../../../../src/util')
+var Vue = require('../../../../src/vue')
+
+if (_.inBrowser) {
+  describe('v-partial', function () {
+
+    var el
+    beforeEach(function () {
+      el = document.createElement('div')
+      spyOn(_, 'warn')
+    })
+
+    it('element', function () {
+      var vm = new Vue({
+        el: el,
+        template: '<div v-partial="test"></div>',
+        partials: {
+          test: '<p>{{a}}</p><p>{{b}}</p>'
+        },
+        data: {
+          a: 'A',
+          b: 'B'
+        }
+      })
+      expect(el.innerHTML).toBe('<div><p>A</p><p>B</p></div>')
+    })
+
+    it('inline', function () {
+      var vm = new Vue({
+        el: el,
+        template: '<div>{{>test}}</div>',
+        partials: {
+          test: '<p>{{a}}</p><p>{{b}}</p>'
+        },
+        data: {
+          a: 'A',
+          b: 'B'
+        }
+      })
+      expect(el.innerHTML).toBe('<div><p>A</p><p>B</p></div>')
+    })
+
+    it('not found', function () {
+      var vm = new Vue({
+        el: el,
+        template: '<div>{{>test}}</div>'
+      })
+      expect(el.innerHTML).toBe('<div><!--v-partial--></div>')
+    })
+
+  })
+}