8000 fix dynamic attribute on custom directive · Mat-Moo/vue@6855198 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6855198

Browse files
committed
fix dynamic attribute on custom directive
1 parent 980f322 commit 6855198

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

src/directive.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,15 @@ Directive.prototype._setupParams = function () {
156156
while (i--) {
157157
key = params[i]
158158
mappedKey = _.camelize(key)
159-
val = _.attr(this.el, key)
159+
val = _.getBindAttr(this.el, key)
160160
if (val != null) {
161-
// static
162-
this.params[mappedKey] = val === '' ? true : val
163-
} else {
164161
// dynamic
165-
val = _.getBindAttr(this.el, key)
162+
this._setupParamWatcher(mappedKey, val)
163+
} else {
164+
// static
165+
val = _.attr(this.el, key)
166166
if (val != null) {
167-
this._setupParamWatcher(mappedKey, val)
167+
this.params[mappedKey] = val === '' ? true : val
168168
}
169169
}
170170
}

test/unit/specs/directive_spec.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ var nextTick = Vue.nextTick
44

55
describe('Directive', function () {
66

7-
var el = {} // simply a mock to be able to run in Node
8-
var vm, def
9-
7+
var el, vm, def
108
beforeEach(function () {
9+
el = document.createElement('div')
1110
def = {
11+
params: ['foo'],
12+
paramWatchers: {
13+
foo: jasmine.createSpy('foo')
14+
},
1215
bind: jasmine.createSpy('bind'),
1316
update: jasmine.createSpy('update'),
1417
unbind: jasmine.createSpy('unbind')
@@ -154,4 +157,32 @@ describe('Directive', function () {
154157
expect(d.update).toBe(def.update)
155158
expect(def.update).toHaveBeenCalled()
156159
})
160+
161+
it('static params', function () {
162+
el.setAttribute('foo', 'hello')
163+
var d = new Directive({
164+
name: 'test',
165+
def: def,
166+
expression: 'a'
167+
}, vm, el)
168+
d._bind()
169+
expect(d.params.foo).toBe('hello')
170+
})
171+
172+
it('dynamic params', function (done) {
173+
el.setAttribute(':foo', 'a')
174+
var d = new Directive({
175+
name: 'test',
176+
def: def,
177+
expression: 'a'
178+
}, vm, el)
179+
d._bind()
180+
expect(d.params.foo).toBe(vm.a)
181+
vm.a = 2
182+
nextTick(function () {
183+
expect(def.paramWatchers.foo).toHaveBeenCalledWith(2, 1)
184+
expect(d.params.foo).toBe(vm.a)
185+
done()
186+
})
187+
})
157188
})

0 commit comments

Comments
 (0)
0