Bläddra i källkod

internalize dep collection to watcher.js

Evan You 11 år sedan
förälder
incheckning
cb9384f9ea
4 ändrade filer med 31 tillägg och 31 borttagningar
  1. 1 1
      src/emitter.js
  2. 0 19
      src/instance/bindings.js
  3. 6 7
      src/instance/init.js
  4. 24 4
      src/watcher.js

+ 1 - 1
src/emitter.js

@@ -71,7 +71,7 @@ p.off = function (event, fn) {
   if (!callbacks) return this
   // remove all handlers
   if (arguments.length === 1) {
-    delete this._cbs[event]
+    this._cbs[event] = null
     return this
   }
   // remove specific handler

+ 0 - 19
src/instance/bindings.js

@@ -11,7 +11,6 @@ var Observer = require('../observe/observer')
  */
 
 exports._initBindings = function () {
-  this._bindings = Object.create(null)
   this._bindings.$data = new Binding()
   // setup observer events
   var update = this._updateBindingAt
@@ -22,8 +21,6 @@ exports._initBindings = function () {
     .on('delete', update)
     // adding properties is a bit different
     .on('add', updateAdd)
-    // collect dependency
-    .on('get', collectDep)
 }
 
 /**
@@ -75,20 +72,4 @@ function updateAdd (path) {
   var index = path.lastIndexOf(Observer.pathDelimiter)
   if (index > -1) path = path.slice(0, index)
   this._updateBindingAt(path)
-}
-
-/**
- * Collect dependency for the target directive being
- * evaluated.
- *
- * @param {String} path
- */
-
-function collectDep (path) {
-  var watcher = this._activeWatcher
-  // the get event might have come from a child vm's watcher
-  // so this._activeWatcher is not guarunteed to be defined
-  if (watcher) {
-    watcher.addDep(path)
-  }
 }

+ 6 - 7
src/instance/init.js

@@ -20,10 +20,10 @@ exports._init = function (options) {
   this.$              = {}
   this.$root          = this.$root || this
   this._emitter       = new Emitter(this)
-  this._watchers      = {}
-  this._userWatchers  = {}
+  this._watchers      = Object.create(null)
+  this._userWatchers  = Object.create(null)
+  this._bindings      = Object.create(null)
   this._directives    = []
-  this._activeWatcher = null
 
   // block instance properties
   this._blockStart  =
@@ -58,14 +58,13 @@ exports._init = function (options) {
   // have been set up & before data observation happens.
   this._callHook('created')
 
-  // initialize data observation and scope inheritance
+  // initialize data observation and scope inheritance.
   this._initScope()
 
-  // setup binding tree.
-  // @creates this._bindings
+  // setup bindings to react to data change.
   this._initBindings()
 
-  // setup event system and option events
+  // setup event system and option events.
   this._initEvents()
 
   // if `el` option is passed, start compilation.

+ 24 - 4
src/watcher.js

@@ -7,6 +7,24 @@ var Batcher = require('./batcher')
 var batcher = new Batcher()
 var uid = 0
 
+/**
+ * Only one watcher will be collecting dependency at
+ * any time.
+ */
+
+var activeWatcher = null
+
+/**
+ * Collect dependency for the target directive being
+ * evaluated. This is called on the active watcher's vm
+ *
+ * @param {String} path
+ */
+
+function collectDep (path) {
+  activeWatcher.addDep(path)
+}
+
 /**
  * A watcher parses an expression, collects dependencies,
  * and fires callback when the expression value changes.
@@ -27,8 +45,8 @@ function Watcher (vm, expression, cb, filters, needSet) {
   this.id = ++uid // uid for batching
   this.value = undefined
   this.active = true
-  this.deps = {}
-  this.newDeps = {}
+  this.deps = Object.create(null)
+  this.newDeps = Object.create(null)
   // setup filters if any.
   // We delegate directive filters here to the watcher
   // because they need to be included in the dependency
@@ -133,7 +151,8 @@ p.set = function (value) {
 
 p.beforeGet = function () {
   Observer.emitGet = true
-  this.vm._activeWatcher = this
+  this.vm.$observer.on('get', collectDep)
+  activeWatcher = this
   this.newDeps = {}
 }
 
@@ -142,8 +161,9 @@ p.beforeGet = function () {
  */
 
 p.afterGet = function () {
-  this.vm._activeWatcher = null
   Observer.emitGet = false
+  this.vm.$observer.off('get')
+  activeWatcher = null
   _.extend(this.newDeps, this.deps)
   this.deps = this.newDeps
 }