Skip to content

Commit 775948d

Browse files
author
Evan You
committed
refactor compile priority order
1 parent c4897fa commit 775948d

File tree

1 file changed

+44
-46
lines changed

1 file changed

+44
-46
lines changed

src/compiler.js

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ var Emitter = require('./emitter'),
2222
'created', 'ready',
2323
'beforeDestroy', 'afterDestroy',
2424
'attached', 'detached'
25+
],
26+
27+
// list of priority directives
28+
// that needs to be checked in specific order
29+
priorityDirectives = [
30+
//'if',
31+
'repeat',
32+
'view',
33+
'component'
2534
]
2635

2736
/**
@@ -324,67 +333,56 @@ CompilerProto.observeData = function (data) {
324333
*/
325334
CompilerProto.compile = function (node, root) {
326335

327-
/* jshint boss: true */
328-
329336
var compiler = this,
330-
nodeType = node.nodeType,
331-
tagName = node.tagName
332-
333-
if (nodeType === 1 && tagName !== 'SCRIPT') { // a normal node
337+
nodeType = node.nodeType
334338

339+
if (nodeType === 1 && node.tagName !== 'SCRIPT') { // a normal node
340+
335341
// skip anything with v-pre
336-
if (utils.attr(node, 'pre') !== null) return
337-
338-
// special attributes to check
339-
var directive, repeatExp, viewExp, Component
340-
341-
// priority order for directives that create child VMs:
342-
// repeat => view => component
343-
344-
if (repeatExp = utils.attr(node, 'repeat')) {
345-
346-
// repeat block cannot have v-id at the same time.
347-
directive = Directive.parse('repeat', repeatExp, compiler, node)
348-
if (directive) {
349-
// defer child component compilation
350-
// so by the time they are compiled, the parent
351-
// would have collected all bindings
352-
compiler.deferred.push(directive)
353-
}
354-
355-
} else if (viewExp = utils.attr(node, 'view')) {
356-
357-
directive = Directive.parse('view', viewExp, compiler, node)
358-
if (directive) {
359-
compiler.deferred.push(directive)
360-
}
361-
362-
} else if (root !== true && (Component = this.resolveComponent(node, undefined, true))) {
342+
if (utils.attr(node, 'pre') !== null) {
343+
return
344+
}
363345

364-
directive = Directive.parse('component', '', compiler, node)
365-
if (directive) {
366-
directive.Ctor = Component
367-
compiler.deferred.push(directive)
346+
// check priority directives.
347+
// if any of them are present, it will take over the node with a childVM
348+
// so we can skip the rest
349+
for (var i = 0, l = priorityDirectives.length; i < l; i++) {
350+
if (compiler.checkPriorityDir(priorityDirectives[i], node, root)) {
351+
return
368352
}
369-
370-
} else {
371-
372-
// compile normal directives
373-
compiler.compileNode(node)
374-
375353
}
376354

377-
} else if (nodeType === 3 && config.interpolate) {
355+
// if none of the priority directives applies,
356+
// compile the normal directives.
357+
compiler.compileNode(node)
378358

359+
} else if (nodeType === 3 && config.interpolate) {
379360
// text node
380361
compiler.compileTextNode(node)
381-
382362
}
363+
}
383364

365+
/**
366+
* Check for a priority directive
367+
* If it is present and valid, return true to skip the rest
368+
*/
369+
CompilerProto.checkPriorityDir = function (dirname, node, root) {
370+
var expression, directive, Ctor
371+
if (dirname === 'component' && root !== true && (Ctor = this.resolveComponent(node, undefined, true))) {
372+
directive = Directive.parse(dirname, '', this, node)
373+
directive.Ctor = Ctor
374+
} else {
375+
expression = utils.attr(node, dirname)
376+
directive = expression && Directive.parse(dirname, expression, this, node)
377+
}
378+
if (directive) {
379+
this.deferred.push(directive)
380+
return true
381+
}
384382
}
385383

386384
/**
387-
* Compile a normal node
385+
* Compile normal directives on a node
388386
*/
389387
CompilerProto.compileNode = function (node) {
390388

0 commit comments

Comments
 (0)