@@ -13,6 +13,7 @@ var Emitter = require('./emitter'),
1313 slice = Array . prototype . slice ,
1414 log = utils . log ,
1515 makeHash = utils . hash ,
16+ extend = utils . extend ,
1617 def = utils . defProtected ,
1718 hasOwn = Object . prototype . hasOwnProperty
1819
@@ -30,21 +31,20 @@ function Compiler (vm, options) {
3031
3132 // process and extend options
3233 options = compiler . options = options || makeHash ( )
34+ var data = compiler . data = options . data || { }
3335 utils . processOptions ( options )
34- utils . extend ( compiler , options . compilerOptions )
36+ extend ( compiler , options . compilerOptions )
37+ extend ( vm , options . data , true )
38+ extend ( vm , options . methods , true )
3539
3640 // initialize element
3741 var el = compiler . setupElement ( options )
3842 log ( '\nnew VM instance:' , el . tagName , '\n' )
3943
40- // init scope
41- var scope = compiler . scope = options . scope || { }
42- utils . extend ( vm , scope , true )
43-
4444 compiler . vm = vm
4545 def ( vm , '$' , makeHash ( ) )
4646 def ( vm , '$el' , el )
47- def ( vm , '$scope ' , scope )
47+ def ( vm , '$data ' , data )
4848 def ( vm , '$compiler' , compiler )
4949
5050 // keep track of directives and expressions
@@ -87,16 +87,16 @@ function Compiler (vm, options) {
8787 // beforeCompile hook
8888 compiler . execHook ( 'beforeCompile' , 'created' )
8989 // the user might have set some props on the vm
90- // so copy it back to the scope ...
91- utils . extend ( scope , vm )
92- // observe the scope
93- Observer . observe ( scope , '' , compiler . observer )
90+ // so copy it back to the data ...
91+ extend ( data , vm )
92+ // observe the data
93+ Observer . observe ( data , '' , compiler . observer )
9494
9595 // for repeated items, create an index binding
9696 // which should be inenumerable but configurable
9797 if ( compiler . repeat ) {
98- //scope .$index = compiler.repeatIndex
99- def ( scope , '$index' , compiler . repeatIndex , false , true )
98+ //data .$index = compiler.repeatIndex
99+ def ( data , '$index' , compiler . repeatIndex , false , true )
100100 compiler . createBinding ( '$index' )
101101 }
102102
@@ -369,7 +369,7 @@ CompilerProto.bindDirective = function (directive) {
369369 // expression bindings are always created on current compiler
370370 binding = compiler . createBinding ( key , true , directive . isFn )
371371 } else if (
372- hasOwn . call ( compiler . scope , baseKey ) ||
372+ hasOwn . call ( compiler . data , baseKey ) ||
373373 hasOwn . call ( compiler . vm , baseKey )
374374 ) {
375375 // If the directive's compiler's VM has the base key,
@@ -409,7 +409,7 @@ CompilerProto.bindDirective = function (directive) {
409409CompilerProto . createBinding = function ( key , isExp , isFn ) {
410410
411411 var compiler = this ,
412- scope = compiler . scope ,
412+ data = compiler . data ,
413413 bindings = compiler . bindings ,
414414 binding = new Binding ( compiler , key , isExp , isFn )
415415
@@ -434,8 +434,8 @@ CompilerProto.createBinding = function (key, isExp, isFn) {
434434 // this is a root level binding. we need to define getter/setters for it.
435435 compiler . define ( key , binding )
436436 } else {
437- // ensure path in scope so it can be observed
438- Observer . ensurePath ( scope , key )
437+ // ensure path in data so it can be observed
438+ Observer . ensurePath ( data , key )
439439 var parentKey = key . slice ( 0 , key . lastIndexOf ( '.' ) )
440440 if ( ! hasOwn . call ( bindings , parentKey ) ) {
441441 // this is a nested value binding, but the binding for its parent
@@ -456,41 +456,40 @@ CompilerProto.define = function (key, binding) {
456456 log ( ' defined root binding: ' + key )
457457
458458 var compiler = this ,
459- scope = compiler . scope ,
459+ data = compiler . data ,
460460 vm = compiler . vm ,
461- value = binding . value = scope [ key ] // save the value before redefinening it
461+ value = binding . value = data [ key ] // save the value before redefinening it
462462
463463 if ( utils . typeOf ( value ) === 'Object' && value . $get ) {
464464 compiler . markComputed ( binding )
465465 }
466466
467- // $index is inenumerable
468- if ( ! ( key in scope ) && key !== '$index' ) {
469- scope [ key ] = undefined
467+ if ( ! ( key in data ) ) {
468+ data [ key ] = undefined
470469 }
471470
472- // if the scope object is already observed, that means
471+ // if the data object is already observed, that means
473472 // this binding is created late. we need to observe it now.
474- if ( scope . __observer__ ) {
475- Observer . convert ( scope , key )
473+ if ( data . __observer__ ) {
474+ Observer . convert ( data , key )
476475 }
477476
478477 Object . defineProperty ( vm , key , {
479478 get : binding . isComputed
480479 ? function ( ) {
481- return scope [ key ] . $get ( )
480+ return data [ key ] . $get ( )
482481 }
483482 : function ( ) {
484- return scope [ key ]
483+ return data [ key ]
485484 } ,
486485 set : binding . isComputed
487486 ? function ( val ) {
488- if ( scope [ key ] . $set ) {
489- scope [ key ] . $set ( val )
487+ if ( data [ key ] . $set ) {
488+ data [ key ] . $set ( val )
490489 }
491490 }
492491 : function ( val ) {
493- scope [ key ] = val
492+ data [ key ] = val
494493 }
495494 } )
496495}
0 commit comments