Skip to content
This repository was archived by the owner on Dec 1, 2023. It is now read-only.

Commit c6da545

Browse files
authored
Merge pull request #4 from dasdeck/master
added named method access to event definitions
2 parents 0837626 + 4b2f0e3 commit c6da545

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

examples/index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff 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

src/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff 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
});

test/event.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff 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
});

0 commit comments

Comments
 (0)