Skip to content

Commit aa566da

Browse files
committed
support new child component ref syntax
1 parent 2ad9554 commit aa566da

File tree

7 files changed

+38
-17
lines changed

7 files changed

+38
-17
lines changed

src/directives/internal/component.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module.exports = {
2525
this.keepAlive = this.param('keep-alive') != null
2626

2727
// check ref
28-
this.ref = this.param('ref')
28+
this.ref = _.findRef(this.el)
2929
var refs = (this._scope || this.vm).$
3030
if (this.ref && !refs.hasOwnProperty(this.ref)) {
3131
_.defineReactive(refs, this.ref, null)

src/directives/public/for.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ module.exports = {
4646
this.idKey = this.param('track-by')
4747

4848
// check ref
49-
this.ref = this.param('ref')
49+
this.ref = _.findRef(this.el)
5050

5151
// check for transition stagger
5252
var stagger = +this.param('stagger')

src/util/dom.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,24 @@ exports.createAnchor = function (content, persist) {
294294
? document.createComment(content)
295295
: document.createTextNode(persist ? ' ' : '')
296296
}
297+
298+
/**
299+
* Find a component ref attribute that starts with $.
300+
*
301+
* @param {Element} node
302+
* @return {String|undefined}
303+
*/
304+
305+
var refRE = /^\$\./
306+
exports.findRef = function (node) {
307+
if (node.hasAttributes()) {
308+
var attrs = node.attributes
309+
for (var i = 0, l = attrs.length; i < l; i++) {
310+
var name = attrs[i].name
311+
if (refRE.test(name)) {
312+
node.removeAttribute(name)
313+
return _.camelize(name.replace(refRE, ''))
314+
}
315+
}
316+
}
317+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ if (_.inBrowser) {
1616
data: {
1717
b: 'B'
1818
},
19-
template: '<test bind-b="b" ref="child"></test>',
19+
template: '<test bind-b="b" $.child></test>',
2020
components: {
2121
test: {
2222
props: ['b'],
@@ -73,7 +73,7 @@ if (_.inBrowser) {
7373
a: 'A'
7474
}
7575
},
76-
template: '<test bind-testt@="test" bind-bb@="b" bind-a@=" test.a " ref="child"></test>',
76+
template: '<test bind-testt@="test" bind-bb@="b" bind-a@=" test.a " $.child></test>',
7777
components: {
7878
test: {
7979
props: ['testt', 'bb', 'a'],
@@ -123,7 +123,7 @@ if (_.inBrowser) {
123123
data: {
124124
b: 'B'
125125
},
126-
template: '<test bind-b*="b" ref="child"></test>',
126+
template: '<test bind-b*="b" $.child></test>',
127127
components: {
128128
test: {
129129
props: ['b'],
@@ -145,7 +145,7 @@ if (_.inBrowser) {
145145
data: {
146146
b: 'B'
147147
},
148-
template: '<test bind-b@=" b + \'B\'" ref="child"></test>',
148+
template: '<test bind-b@=" b + \'B\'" $.child></test>',
149149
components: {
150150
test: {
151151
props: ['b'],

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ if (_.inBrowser) {
2626
data: {
2727
ref: 'test2'
2828
},
29-
template: '<test ref="test"></test><test2 bind-ref="ref"></test2>'
29+
template: '<test $.test></test><test2 $.test-case></test2>'
3030
})
3131
expect(vm.$.test).toBeTruthy()
3232
expect(vm.$.test.$options.id).toBe('test')
33-
expect(vm.$.test2).toBeTruthy()
34-
expect(vm.$.test2.$options.id).toBe('test2')
33+
expect(vm.$.testCase).toBeTruthy()
34+
expect(vm.$.testCase.$options.id).toBe('test2')
3535
})
3636

3737
it('with dynamic component', function (done) {
3838
var vm = new Vue({
3939
el: el,
4040
components: components,
4141
data: { test: 'test' },
42-
template: '<component bind-is="test" ref="test"></component>'
42+
template: '<component bind-is="test" $.test></component>'
4343
})
4444
expect(vm.$.test.$options.id).toBe('test')
4545
vm.test = 'test2'
@@ -57,7 +57,7 @@ if (_.inBrowser) {
5757
var vm = new Vue({
5858
el: el,
5959
data: { view: 'one' },
60-
template: '{{$.test.value}}<component bind-is="view" ref="test"></component>',
60+
template: '{{$.test.value}}<component bind-is="view" $.test></component>',
6161
components: {
6262
one: {
6363
id: 'one',
@@ -96,7 +96,7 @@ if (_.inBrowser) {
9696
el: el,
9797
template:
9898
'<div>' +
99-
'<comp ref="out">{{$.out.msg}}</comp>' +
99+
'<comp $.out>{{$.out.msg}}</comp>' +
100100
'</div>',
101101
components: {
102102
comp: {

test/unit/specs/directives/public/for/for_ref_spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('v-for + ref', function () {
1212
var vm = new Vue({
1313
el: el,
1414
data: { items: [1, 2, 3, 4, 5] },
15-
template: '<test v-for="item in items" bind-item="item" ref="test"></test>',
15+
template: '<test v-for="item in items" bind-item="item" $.test></test>',
1616
components: {
1717
test: {
1818
props: ['item']
@@ -41,7 +41,7 @@ describe('v-for + ref', function () {
4141
b: 2
4242
}
4343
},
44-
template: '<test v-for="item in items" bind-item="item" ref="test"></test>',
44+
template: '<test v-for="item in items" bind-item="item" $.test></test>',
4545
components: {
4646
test: {
4747
props: ['item']
@@ -65,10 +65,10 @@ describe('v-for + ref', function () {
6565
it('nested', function () {
6666
var vm = new Vue({
6767
el: el,
68-
template: '<c1 ref="c1"></c1>',
68+
template: '<c1 $.c1></c1>',
6969
components: {
7070
c1: {
71-
template: '<div v-for="n in 2" ref="c2"></div>'
71+
template: '<div v-for="n in 2" $.c2></div>'
7272
}
7373
}
7474
})

test/unit/specs/misc_spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('Misc', function () {
3434
var spy1 = jasmine.createSpy('attached')
3535
var spy2 = jasmine.createSpy('detached')
3636
var el = document.createElement('div')
37-
el.innerHTML = '<outer ref="outter"><inner></inner></outer>'
37+
el.innerHTML = '<outer $.outter><inner></inner></outer>'
3838
document.body.appendChild(el)
3939

4040
var vm = new Vue({

0 commit comments

Comments
 (0)