Skip to content

Commit 89d32e5

Browse files
author
Evan You
committed
Custom Directive API improvements
- `isLiteral`, `isEmpty` and `isFn` are now explicit options. - bind() now get the initial value as the argument.
1 parent b4cf8bb commit 89d32e5

File tree

4 files changed

+30
-16
lines changed

4 files changed

+30
-16
lines changed

src/compiler.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ CompilerProto.bindDirective = function (directive) {
503503

504504
// for empty or literal directives, simply call its bind()
505505
// and we're done.
506-
if (directive.isEmpty || !directive._update) {
506+
if (directive.isEmpty || directive.isLiteral) {
507507
if (directive.bind) directive.bind()
508508
return
509509
}
@@ -531,13 +531,13 @@ CompilerProto.bindDirective = function (directive) {
531531
binding.dirs.push(directive)
532532
directive.binding = binding
533533

534+
var value = binding.val()
534535
// invoke bind hook if exists
535536
if (directive.bind) {
536-
directive.bind()
537+
directive.bind(value)
537538
}
538-
539539
// set initial value
540-
directive.update(binding.val(), true)
540+
directive.update(value, true)
541541
}
542542

543543
/**

src/directive.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function Directive (definition, expression, rawKey, compiler, node) {
2727
this.vm = compiler.vm
2828
this.el = node
2929

30-
var isEmpty = expression === ''
30+
var isEmpty = expression === ''
3131

3232
// mix in properties from the directive definition
3333
if (typeof definition === 'function') {
@@ -43,7 +43,7 @@ function Directive (definition, expression, rawKey, compiler, node) {
4343
}
4444

4545
// empty expression, we're done.
46-
if (isEmpty) {
46+
if (isEmpty || this.isEmpty) {
4747
this.isEmpty = true
4848
return
4949
}

src/directives/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ module.exports = {
4848
},
4949

5050
cloak: {
51+
isEmpty: true,
5152
bind: function () {
5253
var el = this.el
5354
this.compiler.observer.once('hook:ready', function () {

test/unit/specs/api.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ describe('UNIT: API', function () {
150150
var testId = 'directive-2',
151151
msg = 'wowaaaa?'
152152
dirTest = {
153-
bind: function () {
154-
this.el.setAttribute(testId + 'bind', 'bind')
153+
bind: function (value) {
154+
this.el.setAttribute(testId + 'bind', value + 'bind')
155155
},
156156
update: function (value) {
157157
this.el.setAttribute(testId + 'update', value + 'update')
@@ -167,25 +167,38 @@ describe('UNIT: API', function () {
167167
data: { test: msg }
168168
}),
169169
el = document.querySelector('#' + testId + ' span')
170-
assert.strictEqual(el.getAttribute(testId + 'bind'), 'bind', 'should have called bind()')
171-
assert.strictEqual(el.getAttribute(testId + 'update'), msg + 'update', 'should have called update()')
170+
assert.strictEqual(el.getAttribute(testId + 'bind'), msg + 'bind', 'should have called bind() with value')
171+
assert.strictEqual(el.getAttribute(testId + 'update'), msg + 'update', 'should have called update() with value')
172172
vm.$destroy() // assuming this works
173173
assert.notOk(el.getAttribute(testId + 'bind'), 'should have called unbind()')
174174
})
175175

176-
it('should not bind directive if no update() is provided', function () {
177-
var called = false
176+
it('should not create binding for literal or empty directives', function () {
177+
var literalCalled = false,
178+
emptyCalled = false
178179
Vue.directive('test-literal', {
180+
isLiteral: true,
179181
bind: function () {
180-
called = true
182+
literalCalled = true
181183
assert.strictEqual(this.expression, 'hihi')
182184
assert.notOk(this.binding)
183-
}
185+
},
186+
update: function () {}
187+
})
188+
Vue.directive('test-empty', {
189+
isEmpty: true,
190+
bind: function () {
191+
emptyCalled = true
192+
assert.notOk(this.expression)
193+
assert.notOk(this.binding)
194+
},
195+
update: function () {}
184196
})
185197
new Vue({
186-
template: '<div v-test-literal="hihi"></div>'
198+
template: '<div v-test-literal="hihi"></div><div v-test-empty="123"></div>'
187199
})
188-
assert.ok(called)
200+
assert.ok(literalCalled)
201+
assert.ok(emptyCalled)
189202
})
190203

191204
it('should return directive object/fn if only one arg is given', function () {

0 commit comments

Comments
 (0)