Evan You 12 лет назад
Родитель
Сommit
14d97d4d35
3 измененных файлов с 42 добавлено и 29 удалено
  1. 27 22
      src/binding.js
  2. 2 2
      src/directive.js
  3. 13 5
      src/instance/bindings.js

+ 27 - 22
src/binding.js

@@ -22,35 +22,38 @@ function Binding () {
 
 var p = Binding.prototype
 
-/**
- * Add a child binding to the tree.
- *
- * @param {String} key
- * @param {Binding} child
- */
-
-p.addChild = function (key, child) {
-  this.children[key] = child
-}
-
 /**
  * Traverse along a path and trigger updates
  * along the way.
  *
- * @param {String} path
+ * @param {Array} path
  */
 
 p.updatePath = function (path) {
-  
+  var b = this
+  for (var i = 0, l = path.length; i < l; i++) {
+    if (!b) return
+    b.notify()
+    b = b.children[path[i]]
+  }
+  // for the destination of path, we need to trigger
+  // change for every children. i.e. when an object is
+  // swapped, all its content need to be updated.
+  if (b) {
+    b.updateSubTree()
+  }
 }
 
 /**
  * Trigger updates for the subtree starting at
- * self as root.
+ * self as root. Recursive.
  */
 
-p.updateSubTree = function () {
-  
+p.updateTree = function () {
+  this.notify()
+  for (var key in this.children) {
+    this.children[key].updateTree()
+  }
 }
 
 /**
@@ -59,8 +62,8 @@ p.updateSubTree = function () {
  * @param {Directive} sub
  */
 
-p.addSubscriber = function (sub) {
-  
+p.addSub = function (sub) {
+  this.subs.push(sub)
 }
 
 /**
@@ -69,16 +72,18 @@ p.addSubscriber = function (sub) {
  * @param {Directive} sub
  */
 
-p.removeSubscriber = function (sub) {
-  
+p.removeSub = function (sub) {
+  this.subs.splice(this.subs.indexOf(sub), 1)
 }
 
 /**
  * Notify all subscribers of a new value.
  */
 
-p.publish = function () {
-  
+p.notify = function () {
+  for (var i = 0, l = this.subs.length; i++) {
+    this.subs[i]._update()
+  }
 }
 
 module.exports = Binding

+ 2 - 2
src/directive.js

@@ -4,14 +4,14 @@
  * a list of dependencies (Bindings) and refreshes the list during
  * its getter evaluation.
  *
- * @param {String} id
+ * @param {String} type
  * @param {Node} el
  * @param {Vue} vm
  * @param {String} expression
  * @constructor
  */
 
-function Directive (id, el, vm, expression) {
+function Directive (type, el, vm, expression) {
   
 }
 

+ 13 - 5
src/instance/bindings.js

@@ -40,19 +40,27 @@ exports._initBindings = function () {
 }
 
 /**
- * Create bindings along a path
+ * Create a binding for a given path, and create necessary
+ * parent bindings along the path. Returns the binding at
+ * the end of thep path.
  *
  * @param {Array} path - this should already be a parsed Array.
+ * @return {Binding} - the binding created/retrieved at the destination.
  */
 
-exports._createBindings = function (path) {
+exports._createBindingAt = function (path) {
   var b = this._rootBinding
-  var child
+  var child, key
   for (var i = 0, l = path.length; i < l; i++) {
-    child = new Binding()
-    b.addChild(path[i], child)
+    key = path[i]
+    child = b.children[key]
+    if (!child) {
+      child = new Binding()
+      b.children[key] = child
+    }
     b = child
   }
+  return b
 }
 
 /**