File tree Expand file tree Collapse file tree 3 files changed +32
-4
lines changed Expand file tree Collapse file tree 3 files changed +32
-4
lines changed Original file line number Diff line number Diff line change @@ -67,7 +67,18 @@ The actions syntax follows a pattern of `event:controller#method`.
67
67
68
68
- ` event ` must be the name of a [ _ DOM Event_ ] ( https://developer.mozilla.org/en-US/docs/Web/Events ) , e.g. ` click ` .
69
69
- ` 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.
71
82
72
83
### Multiple Actions
73
84
Original file line number Diff line number Diff line change @@ -95,12 +95,12 @@ type Binding = {type: string; tag: string; method: string}
95
95
function * bindings ( el : Element ) : Iterable < Binding > {
96
96
for ( const action of ( el . getAttribute ( 'data-action' ) || '' ) . trim ( ) . split ( / \s + / ) ) {
97
97
const eventSep = action . lastIndexOf ( ':' )
98
- const methodSep = action . lastIndexOf ( '#' )
98
+ const methodSep = Math . max ( 0 , action . lastIndexOf ( '#' ) ) || action . length
99
99
yield {
100
100
type : action . slice ( 0 , eventSep ) ,
101
101
tag : action . slice ( eventSep + 1 , methodSep ) ,
102
- method : action . slice ( methodSep + 1 )
103
- }
102
+ method : action . slice ( methodSep + 1 ) || 'handleEvent'
103
+ } || 'handleEvent'
104
104
}
105
105
}
106
106
Original file line number Diff line number Diff line change @@ -100,6 +100,23 @@ describe('bind', () => {
100
100
expect ( calls ) . to . have . nested . property ( '[1][0].type' , 'submit' )
101
101
} )
102
102
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
+
103
120
it ( 'can bind multiple actions separated by line feed' , ( ) => {
104
121
const instance = document . createElement ( 'bind-test-element' )
105
122
chai . spy . on ( instance , 'foo' )
You can’t perform that action at this time.
0 commit comments