Browse Source

separate single/multi fragments

Evan You 10 years ago
parent
commit
750ea12726
4 changed files with 102 additions and 102 deletions
  1. 7 4
      src/fragment/factory.js
  2. 0 98
      src/fragment/fragment.js
  3. 52 0
      src/fragment/multi.js
  4. 43 0
      src/fragment/single.js

+ 7 - 4
src/fragment/factory.js

@@ -1,7 +1,8 @@
 var _ = require('../util')
 var compiler = require('../compiler')
 var templateParser = require('../parsers/template')
-var Fragment = require('./fragment')
+var SingleFragment = require('./single')
+var MultiFragment = require('./multi')
 var Cache = require('../cache')
 var linkerCache = new Cache(5000)
 
@@ -44,9 +45,11 @@ function FragmentFactory (vm, el) {
  */
 
 FragmentFactory.prototype.create = function (host, scope) {
-  var el = templateParser.clone(this.template)
-  var unlink = this.linker(this.vm, el, host, scope)
-  return new Fragment(el, unlink)
+  var frag = templateParser.clone(this.template)
+  var unlink = this.linker(this.vm, frag, host, scope)
+  return frag.childNodes.length > 1
+    ? new MultiFragment(frag, unlink)
+    : new SingleFragment(frag.childNodes[0], unlink)
 }
 
 module.exports = FragmentFactory

+ 0 - 98
src/fragment/fragment.js

@@ -1,98 +0,0 @@
-var _ = require('../util')
-
-/**
- * Exposed constructor that returns different fragment type
- * based on fragment childNodes length.
- *
- * @param {DocumentFragment} frag
- * @param {Function} unlink
- */
-
-function Fragment (frag, unlink) {
-  return frag.childNodes.length > 1
-    ? new MultiFragment(frag, unlink)
-    : new SingleFragment(frag.childNodes[0], unlink)
-}
-
-/**
- * Single-node fragment, optimize insertion/removal for
- * single-node repeats.
- *
- * @param {Node} node
- * @param {Function} unlink
- */
-
-function SingleFragment (node, unlink) {
-  this.node = node
-  this.unlink = unlink
-}
-
-/**
- * Insert fragment before target.
- *
- * @param {Node} target
- */
-
-SingleFragment.prototype.before = function (target) {
-  _.before(this.node, target)
-}
-
-/**
- * Remove fragment.
- */
-
-SingleFragment.prototype.remove = function () {
-  _.remove(this.node)
-}
-
-/**
- * Multi-node fragment that has a start and an end node.
- *
- * @param {Node} node
- * @param {Function} unlink
- */
-
-function MultiFragment (frag, unlink) {
-  this.start = _.createAnchor('fragment')
-  this.end = _.createAnchor('fragment')
-  this.frag = frag
-  this.unlink = unlink
-}
-
-/**
- * Insert fragment before target.
- *
- * @param {Node} target
- */
-
-MultiFragment.prototype.before = function (target) {
-  _.before(this.start, target)
-  _.before(this.frag, target)
-  _.before(this.end, target)
-}
-
-/**
- * Remove fragment.
- */
-
-MultiFragment.prototype.remove = function () {
-  var parent = this.start.parentNode
-  var node = this.start.nextSibling
-  while (node !== this.end) {
-    this.frag.appendChild(node)
-  }
-  parent.removeChild(this.start)
-  parent.removeChild(this.end)
-}
-
-/**
- * Shared destroy method
- */
-
-MultiFragment.prototype.destroy =
-SingleFragment.prototype.destroy = function () {
-  this.remove()
-  this.unlink()
-}
-
-module.exports = Fragment

+ 52 - 0
src/fragment/multi.js

@@ -0,0 +1,52 @@
+var _ = require('../util')
+
+/**
+ * Multi-node fragment that has a start and an end node.
+ *
+ * @param {Node} node
+ * @param {Function} unlink
+ */
+
+function MultiFragment (frag, unlink) {
+  this.start = _.createAnchor('fragment')
+  this.end = _.createAnchor('fragment')
+  this.frag = frag
+  this.unlink = unlink
+}
+
+/**
+ * Insert fragment before target.
+ *
+ * @param {Node} target
+ */
+
+MultiFragment.prototype.before = function (target) {
+  _.before(this.start, target)
+  _.before(this.frag, target)
+  _.before(this.end, target)
+}
+
+/**
+ * Remove fragment.
+ */
+
+MultiFragment.prototype.remove = function () {
+  var parent = this.start.parentNode
+  var node = this.start.nextSibling
+  while (node !== this.end) {
+    this.frag.appendChild(node)
+  }
+  parent.removeChild(this.start)
+  parent.removeChild(this.end)
+}
+
+/**
+ * Destroy fragment.
+ */
+
+MultiFragment.prototype.destroy = function () {
+  this.remove()
+  this.unlink()
+}
+
+module.exports = MultiFragment

+ 43 - 0
src/fragment/single.js

@@ -0,0 +1,43 @@
+var _ = require('../util')
+
+/**
+ * Single-node fragment, optimize insertion/removal for
+ * single-node repeats.
+ *
+ * @param {Node} node
+ * @param {Function} unlink
+ */
+
+function SingleFragment (node, unlink) {
+  this.node = node
+  this.unlink = unlink
+}
+
+/**
+ * Insert fragment before target.
+ *
+ * @param {Node} target
+ */
+
+SingleFragment.prototype.before = function (target) {
+  _.before(this.node, target)
+}
+
+/**
+ * Remove fragment.
+ */
+
+SingleFragment.prototype.remove = function () {
+  _.remove(this.node)
+}
+
+/**
+ * Destroy fragment.
+ */
+
+SingleFragment.prototype.destroy = function () {
+  this.remove()
+  this.unlink()
+}
+
+module.exports = SingleFragment