@@ -24,48 +24,45 @@ var Emitter = require('./emitter'),
2424function Compiler ( vm , options ) {
2525
2626 var compiler = this
27-
2827 // indicate that we are intiating this instance
2928 // so we should not run any transitions
3029 compiler . init = true
3130
3231 // process and extend options
3332 options = compiler . options = options || makeHash ( )
34- var data = compiler . data = options . data || { }
3533 utils . processOptions ( options )
36- extend ( compiler , options . compilerOptions )
34+
35+ // copy data, methods & compiler options
36+ var data = compiler . data = options . data || { }
3737 extend ( vm , data , true )
3838 extend ( vm , options . methods , true )
39+ extend ( compiler , options . compilerOptions )
3940
4041 // initialize element
4142 var el = compiler . setupElement ( options )
4243 log ( '\nnew VM instance:' , el . tagName , '\n' )
4344
45+ // set compiler properties
4446 compiler . vm = vm
45- def ( vm , '$' , makeHash ( ) )
46- def ( vm , '$el' , el )
47- def ( vm , '$compiler' , compiler )
48-
49- // keep track of directives and expressions
50- // so they can be unbound during destroy()
5147 compiler . dirs = [ ]
5248 compiler . exps = [ ]
53- compiler . childCompilers = [ ] // keep track of child compilers
54- compiler . emitter = new Emitter ( ) // the emitter used for nested VM communication
55-
56- // Store things during parsing to be processed afterwards,
57- // because we want to have created all bindings before
58- // observing values / parsing dependencies.
59- var computed = compiler . computed = [ ]
49+ compiler . computed = [ ]
50+ compiler . childCompilers = [ ]
51+ compiler . emitter = new Emitter ( )
6052
61- // prototypal inheritance of bindings
53+ // inherit parent bindings
6254 var parent = compiler . parentCompiler
6355 compiler . bindings = parent
6456 ? Object . create ( parent . bindings )
6557 : makeHash ( )
6658 compiler . rootCompiler = parent
6759 ? getRoot ( parent )
6860 : compiler
61+
62+ // set inenumerable VM properties
63+ def ( vm , '$' , makeHash ( ) )
64+ def ( vm , '$el' , el )
65+ def ( vm , '$compiler' , compiler )
6966 def ( vm , '$root' , compiler . rootCompiler . vm )
7067
7168 // set parent VM
@@ -82,13 +79,17 @@ function Compiler (vm, options) {
8279
8380 // setup observer
8481 compiler . setupObserver ( )
82+
8583 // beforeCompile hook
8684 compiler . execHook ( 'beforeCompile' , 'created' )
85+
8786 // the user might have set some props on the vm
8887 // so copy it back to the data...
8988 extend ( data , vm )
89+
9090 // observe the data
9191 Observer . observe ( data , '' , compiler . observer )
92+
9293 // for repeated items, create an index binding
9394 // which should be inenumerable but configurable
9495 if ( compiler . repeat ) {
@@ -97,6 +98,7 @@ function Compiler (vm, options) {
9798 compiler . createBinding ( '$index' )
9899 }
99100
101+ // allow the $data object to be swapped
100102 Object . defineProperty ( vm , '$data' , {
101103 enumerable : false ,
102104 get : function ( ) {
@@ -114,10 +116,15 @@ function Compiler (vm, options) {
114116 // now parse the DOM, during which we will create necessary bindings
115117 // and bind the parsed directives
116118 compiler . compile ( el , true )
119+
117120 // extract dependencies for computed properties
118- if ( computed . length ) DepsParser . parse ( computed )
121+ if ( compiler . computed . length ) {
122+ DepsParser . parse ( compiler . computed )
123+ }
124+
119125 // done!
120126 compiler . init = false
127+
121128 // post compile / ready hook
122129 compiler . execHook ( 'afterCompile' , 'ready' )
123130}
0 commit comments