@@ -24,48 +24,45 @@ var Emitter = require('./emitter'),
24
24
function Compiler ( vm , options ) {
25
25
26
26
var compiler = this
27
-
28
27
// indicate that we are intiating this instance
29
28
// so we should not run any transitions
30
29
compiler . init = true
31
30
32
31
// process and extend options
33
32
options = compiler . options = options || makeHash ( )
34
- var data = compiler . data = options . data || { }
35
33
utils . processOptions ( options )
36
- extend ( compiler , options . compilerOptions )
34
+
35
+ // copy data, methods & compiler options
36
+ var data = compiler . data = options . data || { }
37
37
extend ( vm , data , true )
38
38
extend ( vm , options . methods , true )
39
+ extend ( compiler , options . compilerOptions )
39
40
40
41
// initialize element
41
42
var el = compiler . setupElement ( options )
42
43
log ( '\nnew VM instance:' , el . tagName , '\n' )
43
44
45
+ // set compiler properties
44
46
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()
51
47
compiler . dirs = [ ]
52
48
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 ( )
60
52
61
- // prototypal inheritance of bindings
53
+ // inherit parent bindings
62
54
var parent = compiler . parentCompiler
63
55
compiler . bindings = parent
64
56
? Object . create ( parent . bindings )
65
57
: makeHash ( )
66
58
compiler . rootCompiler = parent
67
59
? getRoot ( parent )
68
60
: compiler
61
+
62
+ // set inenumerable VM properties
63
+ def ( vm , '$' , makeHash ( ) )
64
+ def ( vm , '$el' , el )
65
+ def ( vm , '$compiler' , compiler )
69
66
def ( vm , '$root' , compiler . rootCompiler . vm )
70
67
71
68
// set parent VM
@@ -82,13 +79,17 @@ function Compiler (vm, options) {
82
79
83
80
// setup observer
84
81
compiler . setupObserver ( )
82
+
85
83
// beforeCompile hook
86
84
compiler . execHook ( 'beforeCompile' , 'created' )
85
+
87
86
// the user might have set some props on the vm
88
87
// so copy it back to the data...
89
88
extend ( data , vm )
89
+
90
90
// observe the data
91
91
Observer . observe ( data , '' , compiler . observer )
92
+
92
93
// for repeated items, create an index binding
93
94
// which should be inenumerable but configurable
94
95
if ( compiler . repeat ) {
@@ -97,6 +98,7 @@ function Compiler (vm, options) {
97
98
compiler . createBinding ( '$index' )
98
99
}
99
100
101
+ // allow the $data object to be swapped
100
102
Object . defineProperty ( vm , '$data' , {
101
103
enumerable : false ,
102
104
get : function ( ) {
@@ -114,10 +116,15 @@ function Compiler (vm, options) {
114
116
// now parse the DOM, during which we will create necessary bindings
115
117
// and bind the parsed directives
116
118
compiler . compile ( el , true )
119
+
117
120
// extract dependencies for computed properties
118
- if ( computed . length ) DepsParser . parse ( computed )
121
+ if ( compiler . computed . length ) {
122
+ DepsParser . parse ( compiler . computed )
123
+ }
124
+
119
125
// done!
120
126
compiler . init = false
127
+
121
128
// post compile / ready hook
122
129
compiler . execHook ( 'afterCompile' , 'ready' )
123
130
}
0 commit comments