Skip to content

Commit f4af9f6

Browse files
committed
hoist el
1 parent 8153977 commit f4af9f6

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

src/compiler/compile.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,25 @@ function linkAndCapture (linker, vm) {
8787
var originalDirCount = vm._directives.length
8888
linker()
8989
var dirs = vm._directives.slice(originalDirCount)
90+
var dir
9091
for (var i = 0, l = dirs.length; i < l; i++) {
91-
if (dirs[i].name === 'component') dirs[i]._bind()
92+
dir = dirs[i]
93+
if (dir.name === 'if' ||
94+
dir.name === 'for' ||
95+
dir.name === 'repeat') {
96+
dir._bind()
97+
}
9298
}
9399
for (var i = 0, l = dirs.length; i < l; i++) {
94-
if (dirs[i].name !== 'component') dirs[i]._bind()
100+
dir = dirs[i]
101+
if (dir.name === 'component' ||
102+
dir.name === 'el') {
103+
dir._bind()
104+
}
105+
}
106+
for (var i = 0, l = dirs.length; i < l; i++) {
107+
dir = dirs[i]
108+
if (!dir._bound) dir._bind()
95109
}
96110
return dirs
97111
}
@@ -544,6 +558,8 @@ function compileDirectives (attrs, options) {
544558
_.deprecation.V_ON()
545559
} else if (dirName === 'attr') {
546560
_.deprecation.V_ATTR()
561+
} else if (dirName === 'el') {
562+
_.deprecation.V_EL()
547563
}
548564

549565
}
@@ -556,6 +572,16 @@ function compileDirectives (attrs, options) {
556572
}
557573
} else
558574

575+
// speical case for el
576+
if (name === 'el' || name === 'bind-el') {
577+
dirs.push({
578+
name: 'el',
579+
arg: bindRE.test(name),
580+
descriptors: [newDirParser.parse(value)],
581+
def: options.directives.el
582+
})
583+
} else
584+
559585
// attribute bindings
560586
if (bindRE.test(name)) {
561587
var attributeName = name.replace(bindRE, '')

src/directives/component.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ module.exports = {
4141
_.deprecation.V_REF()
4242
}
4343
this.ref = ref || this.param('ref')
44-
if (this.ref) {
45-
_.defineReactive((this._scope || this.vm).$, this.ref, null)
44+
var refs = (this._scope || this.vm).$
45+
if (this.ref && !refs.hasOwnProperty(this.ref)) {
46+
_.defineReactive(refs, this.ref, null)
4647
}
4748

4849
if (this.keepAlive) {

src/directives/el.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
1+
var _ = require('../util')
2+
13
module.exports = {
24

35
isLiteral: true,
6+
priority: 1000,
47

58
bind: function () {
6-
if (process.env.NODE_ENV !== 'production') {
7-
require('../util').deprecation.V_EL()
9+
var scope = this._scope || this.vm
10+
var refs = scope.$$
11+
var id = this.id = this.arg // bind-el ?
12+
? scope.$eval(this.expression)
13+
: this.expression
14+
if (refs.hasOwnProperty(id)) {
15+
refs[id] = this.el
16+
} else {
17+
_.defineReactive(refs, id, this.el)
818
}
9-
this.vm.$$[this.expression] = this.el
1019
},
1120

1221
unbind: function () {
13-
delete this.vm.$$[this.expression]
22+
(this._scope || this.vm).$$[this.id] = null
1423
}
1524
}

test/unit/specs/directives/el_spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ if (_.inBrowser) {
1313
it('normal', function () {
1414
var vm = new Vue({
1515
el: el,
16-
template: '<div v-el="test" id="test"></div>'
16+
template: '<div el="test" id="test"></div>'
1717
})
1818
expect(vm.$$.test).toBeTruthy()
1919
expect(vm.$$.test.id).toBe('test')
2020
vm._directives[0]._teardown()
21-
expect(vm.$$.test).toBeUndefined()
21+
expect(vm.$$.test).toBeNull()
2222
})
2323

2424
it('with v-repeat', function (done) {

0 commit comments

Comments
 (0)