@@ -96,7 +96,7 @@ function Compiler (vm, options) {
9696 extend ( data , vm )
9797
9898 // observe the data
99- Observer . observe ( data , '' , compiler . observer )
99+ compiler . observeData ( data )
100100
101101 // for repeated items, create an index binding
102102 // which should be inenumerable but configurable
@@ -106,21 +106,6 @@ function Compiler (vm, options) {
106106 compiler . createBinding ( '$index' )
107107 }
108108
109- // allow the $data object to be swapped
110- Object . defineProperty ( vm , '$data' , {
111- enumerable : false ,
112- get : function ( ) {
113- return compiler . data
114- } ,
115- set : function ( newData ) {
116- var oldData = compiler . data
117- Observer . unobserve ( oldData , '' , compiler . observer )
118- compiler . data = newData
119- Observer . copyPaths ( newData , oldData )
120- Observer . observe ( newData , '' , compiler . observer )
121- }
122- } )
123-
124109 // now parse the DOM, during which we will create necessary bindings
125110 // and bind the parsed directives
126111 compiler . compile ( el , true )
@@ -242,6 +227,44 @@ CompilerProto.setupObserver = function () {
242227 }
243228}
244229
230+ CompilerProto . observeData = function ( data ) {
231+
232+ var compiler = this ,
233+ observer = compiler . observer
234+
235+ // recursively observe nested properties
236+ Observer . observe ( data , '' , observer )
237+
238+ // also create binding for top level $data
239+ // so it can be used in templates too
240+ var $dataBinding = compiler . bindings [ '$data' ] = new Binding ( compiler , '$data' )
241+ $dataBinding . update ( data )
242+
243+ // allow $data to be swapped
244+ Object . defineProperty ( compiler . vm , '$data' , {
245+ enumerable : false ,
246+ get : function ( ) {
247+ compiler . observer . emit ( 'get' , '$data' )
248+ return compiler . data
249+ } ,
250+ set : function ( newData ) {
251+ var oldData = compiler . data
252+ Observer . unobserve ( oldData , '' , observer )
253+ compiler . data = newData
254+ Observer . copyPaths ( newData , oldData )
255+ Observer . observe ( newData , '' , observer )
256+ compiler . observer . emit ( 'set' , '$data' , newData )
257+ }
258+ } )
259+
260+ // emit $data change on all changes
261+ observer . on ( 'set' , function ( key ) {
262+ if ( key !== '$data' ) {
263+ $dataBinding . update ( compiler . data )
264+ }
265+ } )
266+ }
267+
245268/**
246269 * Compile a DOM node (recursive)
247270 */
@@ -463,7 +486,6 @@ CompilerProto.bindDirective = function (directive) {
463486 compiler = compiler || this
464487 binding = compiler . bindings [ key ] || compiler . createBinding ( key )
465488 }
466-
467489 binding . instances . push ( directive )
468490 directive . binding = binding
469491
@@ -567,11 +589,10 @@ CompilerProto.defineExp = function (key, binding) {
567589 */
568590CompilerProto . defineComputed = function ( key , binding , value ) {
569591 this . markComputed ( binding , value )
570- var def = {
592+ Object . defineProperty ( this . vm , key , {
571593 get : binding . value . $get ,
572594 set : binding . value . $set
573- }
574- Object . defineProperty ( this . vm , key , def )
595+ } )
575596}
576597
577598/**
0 commit comments