This repository was archived by the owner on Dec 1, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +32
-1
lines changed
Expand file tree Collapse file tree 3 files changed +32
-1
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ <h1>Event Manager</h1>
1919
2020 < button class ="btn btn-primary " type ="button " @click ="triggerGlobalMethod "> Trigger 'test' event using global method</ button >
2121 < button class ="btn btn-primary " type ="button " @click ="triggerInstanceMethod "> Trigger 'test' event using instance method</ button >
22+ < button class ="btn btn-primary " type ="button " @click ="$trigger('pushLog', 'log') "> Trigger 'pushLog' event using instace method</ button >
2223
2324 </ div >
2425
@@ -42,7 +43,9 @@ <h1>Event Manager</h1>
4243
4344 test : function ( fn ) {
4445 this . pushLog ( '\'test\' event executed using ' + fn ) ;
45- }
46+ } ,
47+
48+ pushLog : 'pushLog'
4649
4750 } ,
4851
Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ function initEvents() {
2626 if ( events ) {
2727
2828 forEach ( events , ( listeners , event ) => {
29+
2930 forEach ( isArray ( listeners ) ? listeners : [ listeners ] , listener => {
3031
3132 var priority = 0 ;
@@ -35,6 +36,11 @@ function initEvents() {
3536 listener = listener . handler ;
3637 }
3738
39+ if ( typeof listener === 'string' ) {
40+ const name = listener ;
41+ listener = ( ...args ) => this [ name ] . apply ( this , args ) ;
42+ }
43+
3844 _events . push ( Events . on ( event , listener . bind ( this ) , priority ) ) ;
3945 } ) ;
4046 } ) ;
Original file line number Diff line number Diff line change @@ -138,4 +138,26 @@ describe('Vue.events', () => {
138138
139139 } ) ;
140140
141+ it ( 'Use string names of methods in event list' , ( ) => {
142+ new Vue ( {
143+ data ( ) {
144+ return {
145+ isRun : false
146+ }
147+ } ,
148+ created ( ) {
149+ this . $trigger ( 'test' ) ;
150+ expect ( this . isRun ) . toBe ( true ) ;
151+ } ,
152+ methods : {
153+ test ( ) {
154+ this . isRun = true ;
155+ }
156+ } ,
157+ events : {
158+ test : 'test'
159+ }
160+ } ) ;
161+ } ) ;
162+
141163} ) ;
You can’t perform that action at this time.
0 commit comments