Skip to content

Commit 2ad9554

Browse files
committed
support new element ref syntax
1 parent bab97fd commit 2ad9554

File tree

3 files changed

+20
-33
lines changed

3 files changed

+20
-33
lines changed

src/compiler/compile.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ var resolveAsset = _.resolveAsset
1111
// special binding prefixes
1212
var bindRE = /^bind-|^:/
1313
var onRE = /^on-/
14-
var specialAttrRE = /^(bind-|:)?(el|transition)$/
14+
var transitionRE = /^(bind-|:)?transition$/
15+
var nodeRefRE = /^\$\$\./
1516

1617
// terminal directives
1718
var terminalDirectives = [
@@ -545,12 +546,11 @@ function makeTerminalNodeLinkFn (el, dirName, value, options, def) {
545546
function compileDirectives (attrs, options) {
546547
var i = attrs.length
547548
var dirs = []
548-
var attr, name, value, dirName, dirDef, parsed, isLiteral
549+
var attr, name, value, dirName, dirDef, isLiteral
549550
while (i--) {
550551
attr = attrs[i]
551552
name = attr.name
552553
value = attr.value
553-
parsed = dirParser.parse(value)
554554
// Core directive
555555
if (name.indexOf(config.prefix) === 0) {
556556
dirName = name.slice(config.prefix.length)
@@ -585,14 +585,22 @@ function compileDirectives (attrs, options) {
585585
})
586586
} else
587587

588-
// special attribtues: transition & el
589-
if (specialAttrRE.test(name)) {
588+
// special attribute: transition
589+
if (transitionRE.test(name)) {
590590
dirName = name.replace(bindRE, '')
591591
pushDir(dirName, internalDirectives[dirName], {
592592
literal: !bindRE.test(name)
593593
})
594594
} else
595595

596+
// node ref: $$.xxx
597+
if (nodeRefRE.test(name)) {
598+
value = _.camelize(name.replace(nodeRefRE, ''))
599+
pushDir('el', internalDirectives.el, {
600+
literal: true
601+
})
602+
} else
603+
596604
// attribute bindings
597605
if (bindRE.test(name)) {
598606
dirName = name.replace(bindRE, '')
@@ -615,6 +623,7 @@ function compileDirectives (attrs, options) {
615623
*/
616624

617625
function pushDir (dirName, def, opts) {
626+
var parsed = dirParser.parse(value)
618627
var dir = {
619628
name: dirName,
620629
attr: name,

src/directives/internal/el.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ module.exports = {
55
priority: 1500,
66

77
update: function (id) {
8-
if (this.id) {
9-
this.unbind()
10-
}
118
this.id = id
129
var refs = (this._scope || this.vm).$$
1310
if (refs.hasOwnProperty(id)) {

test/unit/specs/directives/internal/el_spec.js

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,26 @@ if (_.inBrowser) {
1717
data: {
1818
ok: true
1919
},
20-
template: '<div v-if="ok" el="test" id="test"></div>'
20+
template: '<div v-if="ok" $$.test-el id="test"></div>'
2121
})
22-
expect(vm.$$.test).toBeTruthy()
23-
expect(vm.$$.test.id).toBe('test')
22+
expect(vm.$$.testEl).toBeTruthy()
23+
expect(vm.$$.testEl.id).toBe('test')
2424
vm.ok = false
2525
_.nextTick(function () {
26-
expect(vm.$$.test).toBeNull()
26+
expect(vm.$$.testEl).toBeNull()
2727
vm.ok = true
2828
_.nextTick(function () {
29-
expect(vm.$$.test.id).toBe('test')
29+
expect(vm.$$.testEl.id).toBe('test')
3030
done()
3131
})
3232
})
3333
})
3434

35-
it('bind-el', function (done) {
36-
var vm = new Vue({
37-
el: el,
38-
data: {
39-
id: 'test'
40-
},
41-
template: '<div bind-el="id" id="test"></div>'
42-
})
43-
expect(vm.$$.test).toBeTruthy()
44-
expect(vm.$$.test.id).toBe('test')
45-
vm.id = 'changed'
46-
_.nextTick(function () {
47-
expect(vm.$$.test).toBeNull()
48-
expect(vm.$$.changed).toBeTruthy()
49-
expect(vm.$$.changed.id).toBe('test')
50-
done()
51-
})
52-
})
53-
5435
it('inside v-for', function () {
5536
var vm = new Vue({
5637
el: el,
5738
data: { items: [1, 2] },
58-
template: '<div v-for="n in items"><p el="test">{{n}}</p>{{$$.test.textContent}}</div>'
39+
template: '<div v-for="n in items"><p $$.test>{{n}}</p>{{$$.test.textContent}}</div>'
5940
})
6041
expect(vm.$el.textContent).toBe('1122')
6142
})

0 commit comments

Comments
 (0)