Skip to content

Commit 0f3b2e4

Browse files
committed
clean up compiler constructor
1 parent fe27197 commit 0f3b2e4

File tree

3 files changed

+64
-70
lines changed

3 files changed

+64
-70
lines changed

src/compiler.js

Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -43,32 +43,47 @@ function Compiler (vm, options) {
4343

4444
// default state
4545
compiler.init = true
46-
compiler.repeat = false
4746
compiler.destroyed = false
4847

4948
// process and extend options
5049
options = compiler.options = options || makeHash()
5150
utils.processOptions(options)
5251

53-
// copy data, methods & compiler options
54-
var data = compiler.data = options.data || {}
52+
// copy compiler options
5553
extend(compiler, options.compilerOptions)
54+
// repeat indicates this is a v-repeat instance
55+
compiler.repeat = compiler.repeat || false
56+
// expCache will be shared between v-repeat instances
57+
compiler.expCache = compiler.expCache || makeHash()
5658

5759
// initialize element
5860
var el = compiler.el = compiler.setupElement(options)
5961
utils.log('\nnew VM instance: ' + el.tagName + '\n')
6062

61-
// set compiler properties
62-
compiler.vm = el.vue_vm = vm
63-
compiler.bindings = makeHash()
64-
compiler.expCache = compiler.expCache || makeHash()
65-
compiler.dirs = []
66-
compiler.deferred = []
67-
compiler.computed = []
68-
compiler.children = []
69-
compiler.emitter = new Emitter()
70-
compiler.emitter._ctx = vm
71-
compiler.delegators = makeHash()
63+
// set other compiler properties
64+
compiler.vm = el.vue_vm = vm
65+
compiler.bindings = makeHash()
66+
compiler.dirs = []
67+
compiler.deferred = []
68+
compiler.computed = []
69+
compiler.children = []
70+
compiler.emitter = new Emitter(vm)
71+
72+
// create bindings for computed properties
73+
if (options.methods) {
74+
for (key in options.methods) {
75+
compiler.createBinding(key)
76+
}
77+
}
78+
79+
// create bindings for methods
80+
if (options.computed) {
81+
for (key in options.computed) {
82+
compiler.createBinding(key)
83+
}
84+
}
85+
86+
// VM ---------------------------------------------------------------------
7287

7388
// set VM properties
7489
vm.$ = makeHash()
@@ -86,21 +101,14 @@ function Compiler (vm, options) {
86101
}
87102
vm.$root = getRoot(compiler).vm
88103

104+
// DATA -------------------------------------------------------------------
105+
89106
// setup observer
107+
// this is necesarry for all hooks and data observation events
90108
compiler.setupObserver()
91109

92-
// create bindings for computed properties and methods
93-
if (options.methods) {
94-
for (key in options.methods) {
95-
compiler.createBinding(key)
96-
}
97-
}
98-
99-
if (options.computed) {
100-
for (key in options.computed) {
101-
compiler.createBinding(key)
102-
}
103-
}
110+
// initialize data
111+
var data = compiler.data = options.data || {}
104112

105113
// copy paramAttributes
106114
var params = options.paramAttributes
@@ -123,35 +131,51 @@ function Compiler (vm, options) {
123131
// beforeCompile hook
124132
compiler.execHook('created')
125133

126-
// the user might have set some props on the vm
127-
// so copy it back to the data...
134+
// the user might have swapped the data ...
135+
data = compiler.data = vm.$data
136+
137+
// user might also set some properties on the vm
138+
// in which case we should copy back to $data
128139
var vmProp
129140
for (key in vm) {
130141
vmProp = vm[key]
131142
if (
132143
key.charAt(0) !== '$' &&
133-
typeof vmProp !== 'function' &&
134-
data[key] !== vmProp
144+
data[key] !== vmProp &&
145+
typeof vmProp !== 'function'
135146
) {
136147
data[key] = vmProp
137148
}
138149
}
139150

140-
// observe the data
151+
// now we can observe the data.
152+
// this will convert data properties to getter/setters
153+
// and emit the first batch of set events, which will
154+
// in turn create the corresponding bindings.
141155
compiler.observeData(data)
142156

143-
// now parse the DOM, during which we will create necessary bindings
144-
// and bind the parsed directives
157+
// COMPILE ----------------------------------------------------------------
158+
159+
// now parse the DOM and bind directives.
160+
// During this stage, we will also create bindings for
161+
// encountered keypaths that don't have a binding yet.
145162
compiler.compile(el, true)
146163

147-
// bind deferred directives (child components)
164+
// Any directive that creates child VMs are deferred
165+
// so that when they are compiled, all bindings on the
166+
// parent VM have been created.
148167
i = compiler.deferred.length
149168
while (i--) {
150169
compiler.bindDirective(compiler.deferred[i])
151170
}
171+
compiler.deferred = null
152172

153-
// extract dependencies for computed properties
154-
compiler.parseDeps()
173+
// extract dependencies for computed properties.
174+
// this will evaluated all collected computed bindings
175+
// and collect get events that are emitted.
176+
if (this.computed.length) {
177+
DepsParser.parse(this.computed)
178+
}
155179

156180
// done!
157181
compiler.rawContent = null
@@ -766,14 +790,6 @@ CompilerProto.hasKey = function (key) {
766790
hasOwn.call(this.vm, baseKey)
767791
}
768792

769-
/**
770-
* Collect dependencies for computed properties
771-
*/
772-
CompilerProto.parseDeps = function () {
773-
if (!this.computed.length) return
774-
DepsParser.parse(this.computed)
775-
}
776-
777793
/**
778794
* Do a one-time eval of a string that potentially
779795
* includes bindings. It accepts additional raw data
@@ -829,7 +845,6 @@ CompilerProto.destroy = function () {
829845
directives = compiler.dirs,
830846
computed = compiler.computed,
831847
bindings = compiler.bindings,
832-
delegators = compiler.delegators,
833848
children = compiler.children,
834849
parent = compiler.parent
835850

@@ -867,11 +882,6 @@ CompilerProto.destroy = function () {
867882
}
868883
}
869884

870-
// remove all event delegators
871-
for (key in delegators) {
872-
el.removeEventListener(key, delegators[key].handler)
873-
}
874-
875885
// destroy all children
876886
i = children.length
877887
while (i--) {

src/emitter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
function Emitter () {
2-
this._ctx = this
1+
function Emitter (ctx) {
2+
this._ctx = ctx || this
33
}
44

55
var EmitterProto = Emitter.prototype

test/unit/specs/viewmodel.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,7 @@ describe('ViewModel', function () {
410410
expUnbindCalled = false,
411411
bindingUnbindCalled = false,
412412
unobserveCalled = false,
413-
elRemoved = false,
414-
delegatorsRemoved = false
413+
elRemoved = false
415414

416415
var dirMock = {
417416
binding: {
@@ -435,6 +434,7 @@ describe('ViewModel', function () {
435434
}
436435

437436
var compilerMock = {
437+
el: document.createElement('div'),
438438
options: {
439439
beforeDestroy: function () {
440440
beforeDestroyCalled = true
@@ -484,18 +484,6 @@ describe('ViewModel', function () {
484484
},
485485
execHook: function (id) {
486486
this.options[id].call(this)
487-
},
488-
el: {
489-
removeEventListener: function (event, handler) {
490-
assert.strictEqual(event, 'click')
491-
assert.strictEqual(handler, compilerMock.delegators.click.handler)
492-
delegatorsRemoved = true
493-
}
494-
},
495-
delegators: {
496-
click: {
497-
handler: function () {}
498-
}
499487
}
500488
}
501489

@@ -542,10 +530,6 @@ describe('ViewModel', function () {
542530
assert.ok(elRemoved)
543531
})
544532

545-
it('should remove all event delegator listeners', function () {
546-
assert.ok(delegatorsRemoved)
547-
})
548-
549533
})
550534

551535
describe('$data', function () {

0 commit comments

Comments
 (0)