Skip to content

Commit 6673828

Browse files
committed
add isLiteral option for custom directive
1 parent cb1d69c commit 6673828

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/compiler.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,9 @@ CompilerProto.bindDirective = function (directive) {
433433
// keep track of it so we can unbind() later
434434
this.dirs.push(directive)
435435

436-
// for a simple directive, simply call its bind() or _update()
436+
// for empty or literal directives, simply call its bind()
437437
// and we're done.
438-
if (directive.isEmpty) {
438+
if (directive.isEmpty || directive.isLiteral) {
439439
if (directive.bind) directive.bind()
440440
return
441441
}

src/directive.js

+7
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ function Directive (definition, expression, rawKey, compiler, node) {
4848
return
4949
}
5050

51+
// for literal directives, all we need
52+
// is the expression as the value.
53+
if (this.isLiteral) {
54+
this.value = expression.trim()
55+
return
56+
}
57+
5158
this.expression = expression.trim()
5259
this.rawKey = rawKey
5360

test/unit/specs/api.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe('UNIT: API', function () {
6666

6767
var dirTest
6868

69-
it('should create custom directive with set function only', function () {
69+
it('should create custom directive with update() function only', function () {
7070
var testId = 'directive-1',
7171
msg = 'wowow'
7272
Vue.directive('test', function (value) {
@@ -108,6 +108,21 @@ describe('UNIT: API', function () {
108108
assert.notOk(el.getAttribute(testId + 'bind'), 'should have called unbind()')
109109
})
110110

111+
it('should create literal directive if given option', function () {
112+
var called = false
113+
Vue.directive('test-literal', {
114+
isLiteral: true,
115+
bind: function () {
116+
called = true
117+
assert.strictEqual(this.value, 'hihi')
118+
}
119+
})
120+
new Vue({
121+
template: '<div v-test-literal="hihi"></div>'
122+
})
123+
assert.ok(called)
124+
})
125+
111126
it('should return directive object/fn if only one arg is given', function () {
112127
var dir = Vue.directive('test2')
113128
assert.strictEqual(dir, dirTest)

0 commit comments

Comments
 (0)