Skip to content

Commit

Permalink
add isLiteral option for custom directive
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Feb 8, 2014
1 parent cb1d69c commit 6673828
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,9 @@ CompilerProto.bindDirective = function (directive) {
// keep track of it so we can unbind() later
this.dirs.push(directive)

// for a simple directive, simply call its bind() or _update()
// for empty or literal directives, simply call its bind()
// and we're done.
if (directive.isEmpty) {
if (directive.isEmpty || directive.isLiteral) {
if (directive.bind) directive.bind()
return
}
Expand Down
7 changes: 7 additions & 0 deletions src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ function Directive (definition, expression, rawKey, compiler, node) {
return
}

// for literal directives, all we need
// is the expression as the value.
if (this.isLiteral) {
this.value = expression.trim()
return
}

this.expression = expression.trim()
this.rawKey = rawKey

Expand Down
17 changes: 16 additions & 1 deletion test/unit/specs/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('UNIT: API', function () {

var dirTest

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

it('should create literal directive if given option', function () {
var called = false
Vue.directive('test-literal', {
isLiteral: true,
bind: function () {
called = true
assert.strictEqual(this.value, 'hihi')
}
})
new Vue({
template: '<div v-test-literal="hihi"></div>'
})
assert.ok(called)
})

it('should return directive object/fn if only one arg is given', function () {
var dir = Vue.directive('test2')
assert.strictEqual(dir, dirTest)
Expand Down

0 comments on commit 6673828

Please sign in to comment.