Skip to content

Commit 08ff6a9

Browse files
author
User
committed
added Instance Methods/Events APIs
1 parent 732301b commit 08ff6a9

File tree

2 files changed

+80
-4
lines changed

2 files changed

+80
-4
lines changed

apis/instanceevents.js

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,60 @@ const colorPrimitive = custom.colorPrimitive
55
const colorError = custom.colorError
66
const colorArgs = custom.colorArgs
77

8-
const VM_$ON = {}
9-
const VM_$ONCE = {}
10-
const VM_$OFF = {}
11-
const VM_$EMIT = {}
8+
const cat = 'Instance Methods/Events'
9+
10+
const VM_$ON = {
11+
category: cat,
12+
name: `vm.$on(event, callback)`,
13+
arguments:[
14+
`{string | Array<string>} event`,
15+
`{Function} callback`
16+
],
17+
usage: `Listen for a custom event on the current vm.
18+
Events can be triggered by ${colorArgs(`vm.$emit`)}. The callback will receive
19+
all the additional arguments passed into these event-triggering methods.`,
20+
example: `
21+
vm.$on(${chalk.green(`'test'`)}, ${chalk.blue(`function`)} (msg) {
22+
${chalk.green(`console`)}.log(msg)
23+
})
24+
vm.$emit(${chalk.green(`'test'`)}, ${chalk.green(`'hi'`)})
25+
${colorComment(`// -> "hi"`)}
26+
`
27+
}
28+
const VM_$ONCE = {
29+
category: cat,
30+
name: `vm.$once(event,callback)`,
31+
arguments:[
32+
`{string} event`,
33+
`{Function} callback`
34+
],
35+
usage: `Listen for a custom event, but only once.
36+
The listener will be removed once it triggers for the first time.`
37+
}
38+
const VM_$OFF = {
39+
category: cat,
40+
name: `vm.$off([event,callback])`,
41+
arguments: [
42+
`{string} [event]`,
43+
`{Function} [callback]`
44+
],
45+
usage: `Remove custom event listener(s).
46+
* If no arguments are provided, remove all event listeners;
47+
* If only the event is provided, remove all listeners for that event;
48+
* If both event and callback are given, remove the listener
49+
for that specific callback only.
50+
`
51+
}
52+
const VM_$EMIT = {
53+
category: cat,
54+
name: `vm.$emit(event,[...args])`,
55+
arguments: [
56+
`{string} event`,
57+
`[...args]`
58+
],
59+
usage: `Trigger an event on the current instance. Any additional
60+
arguments will be passed into the listener’s callback function.`
61+
}
1262

1363
module.exports = {
1464
VM_$ON,

vue-help.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,29 @@ V.command('vm.$delete', 'Instance Methods/Data: #vm.$delete(target,key)').action
466466
})
467467

468468
// Instance Methods/Events
469+
470+
V.command('vm.$on', 'Instance Methods/Events: #vm.$on(event,callback)').action(function(args, callback){
471+
const w = im[0]
472+
this.log(LI.logInstance(w))
473+
callback()
474+
})
475+
476+
V.command('vm.$once', 'Instance Methods/Events: #vm.$once(event,callback)').action(function(args, callback){
477+
const w = im[1]
478+
this.log(LI.logInstance(w))
479+
callback()
480+
})
481+
482+
V.command('vm.$off', 'Instance Methods/Events: #vm.$off([event,callback])').action(function(args, callback){
483+
const w = im[2]
484+
this.log(LI.logInstance(w))
485+
callback()
486+
})
487+
488+
V.command('vm.$emit', 'Instance Methods/Events: #vm.$delete(event,[...args])').action(function(args, callback){
489+
const w = im[3]
490+
this.log(LI.logInstance(w))
491+
callback()
492+
})
493+
494+
// Instance Methods/Lifecycle

0 commit comments

Comments
 (0)