@@ -96,7 +96,7 @@ function Compiler (vm, options) {
96
96
extend ( data , vm )
97
97
98
98
// observe the data
99
- Observer . observe ( data , '' , compiler . observer )
99
+ compiler . observeData ( data )
100
100
101
101
// for repeated items, create an index binding
102
102
// which should be inenumerable but configurable
@@ -106,21 +106,6 @@ function Compiler (vm, options) {
106
106
compiler . createBinding ( '$index' )
107
107
}
108
108
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
-
124
109
// now parse the DOM, during which we will create necessary bindings
125
110
// and bind the parsed directives
126
111
compiler . compile ( el , true )
@@ -242,6 +227,44 @@ CompilerProto.setupObserver = function () {
242
227
}
243
228
}
244
229
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
+
245
268
/**
246
269
* Compile a DOM node (recursive)
247
270
*/
@@ -463,7 +486,6 @@ CompilerProto.bindDirective = function (directive) {
463
486
compiler = compiler || this
464
487
binding = compiler . bindings [ key ] || compiler . createBinding ( key )
465
488
}
466
-
467
489
binding . instances . push ( directive )
468
490
directive . binding = binding
469
491
@@ -567,11 +589,10 @@ CompilerProto.defineExp = function (key, binding) {
567
589
*/
568
590
CompilerProto . defineComputed = function ( key , binding , value ) {
569
591
this . markComputed ( binding , value )
570
- var def = {
592
+ Object . defineProperty ( this . vm , key , {
571
593
get : binding . value . $get ,
572
594
set : binding . value . $set
573
- }
574
- Object . defineProperty ( this . vm , key , def )
595
+ } )
575
596
}
576
597
577
598
/**
0 commit comments