Skip to content

Commit aa425d6

Browse files
authored
Merge pull request #164 from github/bind-handleevent-default
Method can be ommited from bind defaulting to handleEvent
2 parents 36946da + 7535f5c commit aa425d6

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

docs/_guide/actions.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,18 @@ The actions syntax follows a pattern of `event:controller#method`.
6767

6868
- `event` must be the name of a [_DOM Event_](https://developer.mozilla.org/en-US/docs/Web/Events), e.g. `click`.
6969
- `controller` must be the name of a controller ascendant to the element.
70-
- `method` must be a _public_ _method_ attached to a controller's prototype. Static methods will not work.
70+
- `method` (optional) must be a _public_ _method_ attached to a controller's prototype. Static methods will not work.
71+
72+
If method is not supplied, it will default to `handleEvent`.
73+
74+
Some examples of Actions Syntax:
75+
76+
- `click:my-element#foo` -> `click` events will call `foo` on `my-element` elements.
77+
- `submit:my-element#foo` -> `submit` events will call `foo` on `my-element` elements.
78+
- `click:user-list` -> `click` events will call `handleEvent` on `user-list` elements.
79+
- `click:user-list#` -> `click` events will call `handleEvent` on `user-list` elements.
80+
- `click:top-header-user-profile#` -> `click` events will call `handleEvent` on `top-header-user-profile` elements.
81+
- `nav:keydown:user-list` -> `navigation:keydown` events will call `handleEvent` on `user-list` elements.
7182

7283
### Multiple Actions
7384

src/bind.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,12 @@ type Binding = {type: string; tag: string; method: string}
9595
function* bindings(el: Element): Iterable<Binding> {
9696
for (const action of (el.getAttribute('data-action') || '').trim().split(/\s+/)) {
9797
const eventSep = action.lastIndexOf(':')
98-
const methodSep = action.lastIndexOf('#')
98+
const methodSep = Math.max(0, action.lastIndexOf('#')) || action.length
9999
yield {
100100
type: action.slice(0, eventSep),
101101
tag: action.slice(eventSep + 1, methodSep),
102-
method: action.slice(methodSep + 1)
103-
}
102+
method: action.slice(methodSep + 1) || 'handleEvent'
103+
} || 'handleEvent'
104104
}
105105
}
106106

test/bind.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,23 @@ describe('bind', () => {
100100
expect(calls).to.have.nested.property('[1][0].type', 'submit')
101101
})
102102

103+
it('binds to `handleEvent` is function name is omitted', () => {
104+
const instance = document.createElement('bind-test-element')
105+
chai.spy.on(instance, 'handleEvent')
106+
const el = document.createElement('div')
107+
el.setAttribute('data-action', 'click:bind-test-element submit:bind-test-element')
108+
instance.appendChild(el)
109+
bind(instance)
110+
expect(instance.handleEvent).to.have.not.been.called()
111+
el.dispatchEvent(new CustomEvent('click'))
112+
expect(instance.handleEvent).to.have.been.called.exactly(1)
113+
el.dispatchEvent(new CustomEvent('submit'))
114+
expect(instance.handleEvent).to.have.been.called.exactly(2)
115+
const calls = instance.handleEvent.__spy.calls
116+
expect(calls).to.have.nested.property('[0][0].type', 'click')
117+
expect(calls).to.have.nested.property('[1][0].type', 'submit')
118+
})
119+
103120
it('can bind multiple actions separated by line feed', () => {
104121
const instance = document.createElement('bind-test-element')
105122
chai.spy.on(instance, 'foo')

0 commit comments

Comments
 (0)