Browse Source

optimize components with no data option

Evan You 9 years ago
parent
commit
0fe431b490
2 changed files with 10 additions and 4 deletions
  1. 6 3
      src/core/instance/state.js
  2. 4 1
      src/core/observer/index.js

+ 6 - 3
src/core/instance/state.js

@@ -26,7 +26,11 @@ export function initState (vm: Component) {
   const opts = vm.$options
   if (opts.props) initProps(vm, opts.props)
   if (opts.methods) initMethods(vm, opts.methods)
-  initData(vm)
+  if (opts.data) {
+    initData(vm)
+  } else {
+    observe(vm._data = {}, true /* asRootData */)
+  }
   if (opts.computed) initComputed(vm, opts.computed)
   if (opts.watch) initWatch(vm, opts.watch)
 }
@@ -96,8 +100,7 @@ function initData (vm: Component) {
     }
   }
   // observe data
-  observe(data)
-  data.__ob__ && data.__ob__.vmCount++
+  observe(data, true /* asRootData */)
 }
 
 const computedSharedDefinition = {

+ 4 - 1
src/core/observer/index.js

@@ -103,7 +103,7 @@ function copyAugment (target: Object, src: Object, keys: Array<string>) {
  * returns the new observer if successfully observed,
  * or the existing observer if the value already has one.
  */
-export function observe (value: any): Observer | void {
+export function observe (value: any, asRootData: ?boolean): Observer | void {
   if (!isObject(value)) {
     return
   }
@@ -119,6 +119,9 @@ export function observe (value: any): Observer | void {
   ) {
     ob = new Observer(value)
   }
+  if (asRootData && ob) {
+    ob.vmCount++
+  }
   return ob
 }