@@ -34,7 +34,7 @@ function Compiler (vm, options) {
3434 var data = compiler . data = options . data || { }
3535 utils . processOptions ( options )
3636 extend ( compiler , options . compilerOptions )
37- extend ( vm , options . data , true )
37+ extend ( vm , data , true )
3838 extend ( vm , options . methods , true )
3939
4040 // initialize element
@@ -44,7 +44,6 @@ function Compiler (vm, options) {
4444 compiler . vm = vm
4545 def ( vm , '$' , makeHash ( ) )
4646 def ( vm , '$el' , el )
47- def ( vm , '$data' , data )
4847 def ( vm , '$compiler' , compiler )
4948
5049 // keep track of directives and expressions
@@ -83,15 +82,13 @@ function Compiler (vm, options) {
8382
8483 // setup observer
8584 compiler . setupObserver ( )
86-
8785 // beforeCompile hook
8886 compiler . execHook ( 'beforeCompile' , 'created' )
8987 // the user might have set some props on the vm
9088 // so copy it back to the data...
9189 extend ( data , vm )
9290 // observe the data
9391 Observer . observe ( data , '' , compiler . observer )
94-
9592 // for repeated items, create an index binding
9693 // which should be inenumerable but configurable
9794 if ( compiler . repeat ) {
@@ -100,16 +97,27 @@ function Compiler (vm, options) {
10097 compiler . createBinding ( '$index' )
10198 }
10299
100+ Object . defineProperty ( vm , '$data' , {
101+ enumerable : false ,
102+ get : function ( ) {
103+ return compiler . data
104+ } ,
105+ set : function ( newData ) {
106+ var oldData = compiler . data
107+ Observer . unobserve ( oldData , '' , compiler . observer )
108+ compiler . data = newData
109+ Observer . copyPaths ( newData , oldData )
110+ Observer . observe ( newData , '' , compiler . observer )
111+ }
112+ } )
113+
103114 // now parse the DOM, during which we will create necessary bindings
104115 // and bind the parsed directives
105116 compiler . compile ( el , true )
106-
107117 // extract dependencies for computed properties
108118 if ( computed . length ) DepsParser . parse ( computed )
109-
110119 // done!
111120 compiler . init = false
112-
113121 // post compile / ready hook
114122 compiler . execHook ( 'afterCompile' , 'ready' )
115123}
@@ -409,7 +417,6 @@ CompilerProto.bindDirective = function (directive) {
409417CompilerProto . createBinding = function ( key , isExp , isFn ) {
410418
411419 var compiler = this ,
412- data = compiler . data ,
413420 bindings = compiler . bindings ,
414421 binding = new Binding ( compiler , key , isExp , isFn )
415422
@@ -435,7 +442,7 @@ CompilerProto.createBinding = function (key, isExp, isFn) {
435442 compiler . define ( key , binding )
436443 } else {
437444 // ensure path in data so it can be observed
438- Observer . ensurePath ( data , key )
445+ Observer . ensurePath ( compiler . data , key )
439446 var parentKey = key . slice ( 0 , key . lastIndexOf ( '.' ) )
440447 if ( ! hasOwn . call ( bindings , parentKey ) ) {
441448 // this is a nested value binding, but the binding for its parent
@@ -477,19 +484,19 @@ CompilerProto.define = function (key, binding) {
477484 Object . defineProperty ( vm , key , {
478485 get : binding . isComputed
479486 ? function ( ) {
480- return data [ key ] . $get ( )
487+ return compiler . data [ key ] . $get ( )
481488 }
482489 : function ( ) {
483- return data [ key ]
490+ return compiler . data [ key ]
484491 } ,
485492 set : binding . isComputed
486493 ? function ( val ) {
487- if ( data [ key ] . $set ) {
488- data [ key ] . $set ( val )
494+ if ( compiler . data [ key ] . $set ) {
495+ compiler . data [ key ] . $set ( val )
489496 }
490497 }
491498 : function ( val ) {
492- data [ key ] = val
499+ compiler . data [ key ] = val
493500 }
494501 } )
495502}
0 commit comments