Evan You il y a 11 ans
Parent
commit
77c9b8bfe7
3 fichiers modifiés avec 15 ajouts et 22 suppressions
  1. 5 0
      src/instance/init.js
  2. 10 8
      src/instance/scope.js
  3. 0 14
      src/vue.js

+ 5 - 0
src/instance/init.js

@@ -45,6 +45,11 @@ exports._init = function (options) {
   this._children = []
   if (this.$parent) {
     this.$parent._children.push(this)
+    var root = this.$parent
+    while (root.$parent) {
+      root = root.$parent
+    }
+    this.$root = root
   }
 
   // merge options.

+ 10 - 8
src/instance/scope.js

@@ -20,8 +20,10 @@ exports._initScope = function () {
     ? Object.create(parent.$scope)
     : {}
   // copy initial data into scope
-  for (var key in data) {
+  var keys = Object.keys(data)
+  for (var key, i = 0, l = keys.length; i < l; i++) {
     // use defineProperty so we can shadow parent accessors
+    key = keys[i]
     _.define(scope, key, data[key], true)
   }
   // create scope observer
@@ -138,10 +140,9 @@ exports._initProxy = function () {
   // scope --> vm
 
   // proxy scope data on vm
-  for (var key in scope) {
-    if (scope.hasOwnProperty(key)) {
-      _.proxy(this, scope, key)
-    }
+  var keys = Object.keys(scope)
+  for (var i = 0, l = keys.length; i < l; i++) {
+    _.proxy(this, scope, keys[i])
   }
   // keep proxying up-to-date with added/deleted keys.
   this.$observer
@@ -154,9 +155,10 @@ exports._initProxy = function () {
 
   // vm --> scope
 
-  // proxy vm parent & root on scope
-  _.proxy(scope, this, '$parent')
-  _.proxy(scope, this, '$root')
+  // $parent & $root are read-only on $scope
+  scope.$parent = this.$parent
+  scope.$root = this.$root
+  // proxy $data
   _.proxy(scope, this, '$data')
 }
 

+ 0 - 14
src/vue.js

@@ -48,20 +48,6 @@ Vue.options = {
 
 var p = Vue.prototype
 
-/**
- * The $root recursively points to the root instance.
- *
- * @readonly
- */
-
-Object.defineProperty(p, '$root', {
-  get: function () {
-    return this.$parent
-      ? this.$parent.$root
-      : this
-  }
-})
-
 /**
  * $data has a setter which does a bunch of
  * teardown/setup work