Просмотр исходного кода

move data-related funcs into one file

Evan You 12 лет назад
Родитель
Сommit
f80c22fc3b
6 измененных файлов с 140 добавлено и 107 удалено
  1. 13 0
      changes.md
  2. 124 0
      src/instance/data.js
  3. 3 0
      src/instance/init.js
  4. 0 33
      src/instance/proxy.js
  5. 0 72
      src/instance/scope.js
  6. 0 2
      src/vue.js

+ 13 - 0
changes.md

@@ -54,6 +54,19 @@ This new hook is introduced to accompany the separation of instantiation and DOM
 
 These two have caused confusions about when they'd actually fire, and proper use cases seem to be rare. Let me know if you have important use cases for these two hooks.
 
+## Computed Properties
+
+`$get` and `$set` is now simply `get` and `set`:
+
+``` js
+computed: {
+  fullName: {
+    get: function () {},
+    set: function () {}
+  }
+}
+```
+
 ## Two Way filters
 
 If a filter is defined as a function, it is treated as a read filter by default - i.e. it is applied when data is read from the model and applied to the DOM. You can now specify write filters as well, which are applied when writing to the model, triggered by user input. Write filters are only triggered on two-way bindings like `v-model`.

+ 124 - 0
src/instance/data.js

@@ -1,4 +1,76 @@
+var _ = require('../util')
 var Observer = require('../observe/observer')
+var scopeEvents = ['set', 'mutate', 'add', 'delete']
+
+/**
+ * Setup instance scope.
+ * The scope is reponsible for prototypal inheritance of
+ * parent instance propertiesm abd all binding paths and
+ * expressions of the current instance are evaluated against its scope.
+ *
+ * This should only be called once during _init().
+ */
+
+exports._initScope = function () {
+  var parent = this.$parent
+  var scope = this.$scope = parent
+    ? Object.create(parent.$scope)
+    : {}
+  // create scope observer
+  this._observer = Observer.create(scope, {
+    callbackContext: this,
+    doNotAlterProto: true
+  })
+
+  if (!parent) return
+
+  // scope parent accessor
+  Object.defineProperty(scope, '$parent', {
+    get: function () {
+      return parent.$scope
+    }
+  })
+
+  // scope root accessor
+  var self = this
+  Object.defineProperty(scope, '$root', {
+    get: function () {
+      return self.$root.$scope
+    }
+  })
+
+  // relay change events that sent down from
+  // the scope prototype chain.
+  var ob = this._observer
+  var pob = parent._observer
+  var listeners = this._scopeListeners = {}
+  scopeEvents.forEach(function (event) {
+    var cb = listeners[event] = function (key, a, b) {
+      // since these events come from upstream,
+      // we only emit them if we don't have the same keys
+      // shadowing them in current scope.
+      if (!scope.hasOwnProperty(key)) {
+        ob.emit(event, key, a, b)
+      }
+    }
+    pob.on(event, cb)
+  })
+}
+
+/**
+ * Teardown scope and remove listeners attached to parent scope.
+ * Only called once during $destroy().
+ */
+
+exports._teardownScope = function () {
+  this.$scope = null
+  if (!this.$parent) return
+  var pob = this.$parent._observer
+  var listeners = this._scopeListeners
+  scopeEvents.forEach(function (event) {
+    pob.off(event, listeners[event])
+  })
+}
 
 /**
  * Setup the instances data object.
@@ -55,6 +127,58 @@ exports._initData = function (data, init) {
   }
 }
 
+/**
+ * Setup computed properties.
+ */
+
+exports._initComputed = function () {
+  var computed = this.$options.computed
+  var scope = this.$scope
+  if (computed) {
+    for (var key in computed) {
+      var def = computed[key]
+      if (typeof def === 'function') {
+        def = { get: def }
+      }
+      def.enumerable = true
+      def.configurable = true
+      Object.defineProperty(scope, key, def)
+    }
+  }
+}
+
+/**
+ * Proxy the scope properties on the instance itself,
+ * so that vm.a === vm.$scope.a.
+ *
+ * Note this only proxies *local* scope properties. We want to
+ * prevent child instances accidentally modifying properties
+ * with the same name up in the scope chain because scope
+ * perperties are all getter/setters.
+ *
+ * To access parent properties through prototypal fall through,
+ * access it on the instance's $scope.
+ *
+ * This should only be called once during _init().
+ */
+
+exports._initProxy = function () {
+  var scope = this.$scope
+  for (var key in scope) {
+    if (scope.hasOwnProperty(key)) {
+      _.proxy(this, scope, key)
+    }
+  }
+  // keep proxying up-to-date with added/deleted keys.
+  this._observer
+    .on('add:self', function (key) {
+      _.proxy(this, scope, key)
+    })
+    .on('delete:self', function (key) {
+      delete this[key]
+    })
+}
+
 /**
  * Setup two-way sync between the instance scope and
  * the original data. Requires teardown.

+ 3 - 0
src/instance/init.js

@@ -50,6 +50,9 @@ exports._init = function (options) {
   // setup initial data.
   this._initData(this._data, true)
 
+  // setup computed properties
+  this._initComputed()
+
   // setup property proxying
   this._initProxy()
 

+ 0 - 33
src/instance/proxy.js

@@ -1,33 +0,0 @@
-var _ = require('../util')
-
-/**
- * Proxy the scope properties on the instance itself,
- * so that vm.a === vm.$scope.a.
- *
- * Note this only proxies *local* scope properties. We want to
- * prevent child instances accidentally modifying properties
- * with the same name up in the scope chain because scope
- * perperties are all getter/setters.
- *
- * To access parent properties through prototypal fall through,
- * access it on the instance's $scope.
- *
- * This should only be called once during _init().
- */
-
-exports._initProxy = function () {
-  var scope = this.$scope
-  for (var key in scope) {
-    if (scope.hasOwnProperty(key)) {
-      _.proxy(this, scope, key)
-    }
-  }
-  // keep proxying up-to-date with added/deleted keys.
-  this._observer
-    .on('add:self', function (key) {
-      _.proxy(this, scope, key)
-    })
-    .on('delete:self', function (key) {
-      delete this[key]
-    })
-}

+ 0 - 72
src/instance/scope.js

@@ -1,72 +0,0 @@
-var Observer = require('../observe/observer')
-var scopeEvents = ['set', 'mutate', 'add', 'delete']
-
-/**
- * Setup instance scope.
- * The scope is reponsible for prototypal inheritance of
- * parent instance propertiesm abd all binding paths and
- * expressions of the current instance are evaluated against its scope.
- *
- * This should only be called once during _init().
- */
-
-exports._initScope = function () {
-  var parent = this.$parent
-  var scope = this.$scope = parent
-    ? Object.create(parent.$scope)
-    : {}
-  // create scope observer
-  this._observer = Observer.create(scope, {
-    callbackContext: this,
-    doNotAlterProto: true
-  })
-
-  if (!parent) return
-
-  // scope parent accessor
-  Object.defineProperty(scope, '$parent', {
-    get: function () {
-      return parent.$scope
-    }
-  })
-
-  // scope root accessor
-  var self = this
-  Object.defineProperty(scope, '$root', {
-    get: function () {
-      return self.$root.$scope
-    }
-  })
-
-  // relay change events that sent down from
-  // the scope prototype chain.
-  var ob = this._observer
-  var pob = parent._observer
-  var listeners = this._scopeListeners = {}
-  scopeEvents.forEach(function (event) {
-    var cb = listeners[event] = function (key, a, b) {
-      // since these events come from upstream,
-      // we only emit them if we don't have the same keys
-      // shadowing them in current scope.
-      if (!scope.hasOwnProperty(key)) {
-        ob.emit(event, key, a, b)
-      }
-    }
-    pob.on(event, cb)
-  })
-}
-
-/**
- * Teardown scope and remove listeners attached to parent scope.
- * Only called once during $destroy().
- */
-
-exports._teardownScope = function () {
-  this.$scope = null
-  if (!this.$parent) return
-  var pob = this.$parent._observer
-  var listeners = this._scopeListeners
-  scopeEvents.forEach(function (event) {
-    pob.off(event, listeners[event])
-  })
-}

+ 0 - 2
src/vue.js

@@ -56,9 +56,7 @@ Object.defineProperty(p, '$data', {
 
 extend(p, require('./instance/init'))
 extend(p, require('./instance/events'))
-extend(p, require('./instance/scope'))
 extend(p, require('./instance/data'))
-extend(p, require('./instance/proxy'))
 extend(p, require('./instance/bindings'))
 extend(p, require('./instance/element'))
 extend(p, require('./instance/compile'))