Skip to content

Commit 8cd19f0

Browse files
author
Evan You
committed
allow one-time interpolation inside all directives
1 parent 163f435 commit 8cd19f0

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

src/directive.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ var dirId = 1,
33
FILTER_TOKEN_RE = /[^\s'"]+|'[^']+'|"[^"]+"/g,
44
NESTING_RE = /^\$(parent|root)\./,
55
SINGLE_VAR_RE = /^[\w\.$]+$/,
6-
QUOTE_RE = /"/g
6+
QUOTE_RE = /"/g,
7+
TextParser = require('./text-parser')
78

89
/**
910
* Directive class
@@ -38,11 +39,12 @@ function Directive (name, ast, definition, compiler, el) {
3839
return
3940
}
4041

41-
this.expression = (
42-
this.isLiteral
43-
? compiler.eval(this.expression)
44-
: this.expression
45-
).trim()
42+
if (TextParser.Regex.test(this.key)) {
43+
this.key = compiler.eval(this.key)
44+
if (this.isLiteral) {
45+
this.expression = this.key
46+
}
47+
}
4648

4749
var filters = ast.filters,
4850
filter, fn, i, l, computed

src/text-parser.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
var openChar = '{',
22
endChar = '}',
33
ESCAPE_RE = /[-.*+?^${}()|[\]\/\\]/g,
4-
BINDING_RE = buildInterpolationRegex(),
54
// lazy require
65
Directive
76

7+
exports.Regex = buildInterpolationRegex()
8+
89
function buildInterpolationRegex () {
910
var open = escapeRegex(openChar),
1011
end = escapeRegex(endChar)
@@ -16,10 +17,10 @@ function escapeRegex (str) {
1617
}
1718

1819
function setDelimiters (delimiters) {
19-
exports.delimiters = delimiters
2020
openChar = delimiters[0]
2121
endChar = delimiters[1]
22-
BINDING_RE = buildInterpolationRegex()
22+
exports.delimiters = delimiters
23+
exports.Regex = buildInterpolationRegex()
2324
}
2425

2526
/**
@@ -30,10 +31,10 @@ function setDelimiters (delimiters) {
3031
* 3. object with key & html = true
3132
*/
3233
function parse (text) {
33-
if (!BINDING_RE.test(text)) return null
34+
if (!exports.Regex.test(text)) return null
3435
var m, i, token, match, tokens = []
3536
/* jshint boss: true */
36-
while (m = text.match(BINDING_RE)) {
37+
while (m = text.match(exports.Regex)) {
3738
i = m.index
3839
if (i > 0) tokens.push(text.slice(0, i))
3940
token = { key: m[1].trim() }

test/unit/specs/directive.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ describe('Directive', function () {
1313
}
1414
}
1515

16+
function makeSpy () {
17+
var spy = function () {
18+
spy.called++
19+
spy.args = [].slice.call(arguments)
20+
}
21+
spy.called = 0
22+
return spy
23+
}
24+
1625
function build (name, exp, compiler) {
1726
var ast = Directive.parse(exp)[0]
1827
return new Directive(name, ast, directives[name], compiler, {})
@@ -259,6 +268,13 @@ describe('Directive', function () {
259268
assert.strictEqual(f3.name, 'lowercase')
260269
})
261270

271+
it('should compile the key if the key contains interpolation tags', function () {
272+
compiler.eval = makeSpy()
273+
build('model', '{{abc}} | uppercase', compiler)
274+
assert.ok(compiler.eval.called)
275+
assert.equal(compiler.eval.args[0], '{{abc}}')
276+
})
277+
262278
})
263279

264280
describe('.$applyFilters()', function () {

test/unit/specs/misc.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,19 @@ describe('Misc Features', function () {
270270

271271
})
272272

273+
describe('interpolation in directive values', function () {
274+
275+
it('should be evaled by the compiler', function () {
276+
var t = new Vue({
277+
template: '<input v-model="{{field}}">',
278+
data: {
279+
field: 'test',
280+
test: 'hello'
281+
}
282+
})
283+
assert.equal(t.$el.childNodes[0].value, 'hello')
284+
})
285+
286+
})
287+
273288
})

0 commit comments

Comments
 (0)