Skip to content

Commit 6b98ebd

Browse files
committed
support new ref syntax
1 parent fe4dfd0 commit 6b98ebd

File tree

9 files changed

+42
-19
lines changed

9 files changed

+42
-19
lines changed

src/directive.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,6 @@ Directive.prototype.param = function (name) {
208208
if (param != null) {
209209
this.el.removeAttribute(name)
210210
param = (this._scope || this.vm).$interpolate(param)
211-
} else {
212-
param = _.getBindAttr(this.el, name)
213-
if (param != null) {
214-
param = (this._scope || this.vm).$eval(param)
215-
process.env.NODE_ENV !== 'production' && _.log(
216-
'You are using bind- syntax on "' + name + '", which ' +
217-
'is a directive param. It will be evaluated only once.'
218-
)
219-
}
220211
}
221212
return param
222213
}

src/directives/component.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ module.exports = {
4141
if (process.env.NODE_ENV !== 'production' && ref) {
4242
_.deprecation.V_REF()
4343
}
44-
this.ref = ref || this.param('ref')
44+
this.ref = ref || _.findRef(this.el)
4545
var refs = (this._scope || this.vm).$
4646
if (this.ref && !refs.hasOwnProperty(this.ref)) {
4747
_.defineReactive(refs, this.ref, null)

src/directives/el.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ module.exports = {
66
priority: 1500,
77

88
bind: function () {
9-
if (this.arg) {
10-
this._isDynamicLiteral = true
11-
} else {
9+
if (!this._isDynamicLiteral) {
1210
this.update(this.expression)
1311
}
1412
},

src/directives/for.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module.exports = {
5555
if (process.env.NODE_ENV !== 'production' && ref) {
5656
_.deprecation.V_REF()
5757
}
58-
this.ref = ref || this.param('ref')
58+
this.ref = ref || _.findRef(this.el)
5959

6060
// check for transition stagger
6161
var stagger = +this.param('stagger')

src/directives/repeat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ module.exports = {
7575
if (this.refId) _.deprecation.V_REF()
7676
if (this.elID) _.deprecation.V_EL()
7777
}
78-
this.refId = this.refId || this.param('ref')
78+
this.refId = this.refId || _.findRef(this.el)
7979

8080
// check other directives that need to be handled
8181
// at v-repeat level

src/util/dom.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@ exports.getBindAttr = function (node, name) {
7979
return val
8080
}
8181

82+
var refRE = /^\$\./
83+
exports.findRef = function (node) {
84+
if (node.hasAttributes()) {
85+
var attrs = node.attributes
86+
for (var i = 0, l = attrs.length; i < l; i++) {
87+
var name = attrs[i].name
88+
if (refRE.test(name)) {
89+
node.removeAttribute(name)
90+
return name.replace(refRE, '')
91+
}
92+
}
93+
}
94+
}
95+
8296
/**
8397
* Insert el before target
8498
*

src/util/lang.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ exports.define = function (obj, key, val, enumerable) {
236236
exports.defineReactive = function (obj, key, val) {
237237
var dep = new Dep()
238238
Object.defineProperty(obj, key, {
239+
enumerable: true,
239240
get: function metaGetter () {
240241
if (Dep.target) {
241242
dep.depend()

test/unit/specs/directives/el_spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,25 @@ if (_.inBrowser) {
2222
expect(vm.$$.test).toBeNull()
2323
})
2424

25+
it('dynamic', function (done) {
26+
var vm = new Vue({
27+
el: el,
28+
data: {
29+
id: 'test'
30+
},
31+
template: '<div v-el="{{id}}" id="test"></div>'
32+
})
33+
expect(vm.$$.test).toBeTruthy()
34+
expect(vm.$$.test.id).toBe('test')
35+
vm.id = 'changed'
36+
_.nextTick(function () {
37+
expect(vm.$$.test).toBeNull()
38+
expect(vm.$$.changed).toBeTruthy()
39+
expect(vm.$$.changed.id).toBe('test')
40+
done()
41+
})
42+
})
43+
2544
it('normal (new syntax)', function (done) {
2645
var vm = new Vue({
2746
el: el,

test/unit/specs/directives/ref_spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ 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 v-ref="{{ref}}"></test2>'
3030
})
3131
expect(vm.$.test).toBeTruthy()
3232
expect(vm.$.test.$options.id).toBe('test')
@@ -39,7 +39,7 @@ if (_.inBrowser) {
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: {

0 commit comments

Comments
 (0)