Skip to content

Commit ae9ebd5

Browse files
author
User
committed
updated Instance Methods/Lifecycle APIs
1 parent 900e7e5 commit ae9ebd5

File tree

4 files changed

+123
-8
lines changed

4 files changed

+123
-8
lines changed

API.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const O_M = require('./apis/optionsmisc')
99
const I_P = require('./apis/instanceproperties')
1010
const I_D = require('./apis/instancedata')
1111
const I_E = require('./apis/instanceevents')
12+
const I_L = require('./apis/instancelifecycle')
1213

1314

1415
const GlobalConfig = [
@@ -119,6 +120,13 @@ const InstanceEvents = [
119120
I_E.VM_$EMIT
120121
]
121122

123+
const InstanceLifecycle = [
124+
I_L.VM_$MOUNT,
125+
I_L.VM_$FORCE_UPDATE,
126+
I_L.VM_$NEXT_TICK,
127+
I_L.VM_$DESTROY
128+
]
129+
122130

123131
module.exports = {
124132
GlobalConfig,
@@ -131,5 +139,6 @@ module.exports = {
131139
OptionsMisc,
132140
InstanceProperties,
133141
InstanceData,
134-
InstanceEvents
142+
InstanceEvents,
143+
InstanceLifecycle
135144
}

apis/instancelifecycle.js

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,91 @@ const colorArgs = custom.colorArgs
77

88
const cat = 'Instance Methods/Lifecycle'
99

10-
const VM_$MOUNT = {}
11-
const VM_$FORCE_UPDATE = {}
12-
const VM_$NEXT_TICK = {}
13-
const VM_$DESTROY = {}
10+
const VM_$MOUNT = {
11+
category: cat,
12+
name: `vm.$mount([elementOrSelector])`,
13+
arguments: [
14+
`{Element | string} [elementOrSelector]`,
15+
`{boolean} [hydrating]`,
16+
],
17+
returns: `vm - the instance itself`,
18+
usage: `If a Vue instance didn’t receive the ${colorArgs(`el`)} option at instantiation,
19+
it will be in “unmounted” state, without an associated DOM element.
20+
${colorArgs(`vm.$mount()`)} can be used to manually start the mounting of
21+
an unmounted Vue instance. If ${colorArgs(`elementOrSelector`)} argument
22+
is not provided, the template will be rendered as an off-document
23+
element, and you will have to use native DOM API to insert it
24+
into the document yourself. The method returns the instance itself
25+
so you can chain other instance methods after it.`,
26+
example: `
27+
${colorArgs(`var`)} MyComponent = Vue.extend({
28+
template: ${chalk.green("'<div>Hello!</div>'")}
29+
})
30+
${colorComment(`// create and mount to #app (will replace #app)`)}
31+
${colorArgs(`new`)} MyComponent().$mount(${chalk.green("'#app'")})
32+
${colorComment(`// the above is the same as:`)}
33+
${colorArgs(`new`)} MyComponent({ el: '#app' })
34+
${colorComment(`// or, render off-document and append afterwards:`)}
35+
${colorArgs(`var`)} component = ${colorArgs(`new`)} MyComponent().$mount()
36+
${chalk.green("document")}.getElementById(${chalk.green("'app'")}).appendChild(component.$el)
37+
`
38+
}
39+
40+
const VM_$FORCE_UPDATE = {
41+
category: cat,
42+
name: `vm.$forceUpdate()`,
43+
usage: `Force the Vue instance to re-render. Note it does not affect all
44+
child components, only the instance itself and child components
45+
with inserted slot content.`
46+
}
47+
48+
const VM_$NEXT_TICK = {
49+
category: cat,
50+
name: `vm.$nextTick([callback])`,
51+
arguments: [
52+
`{Function} [callback]`
53+
],
54+
usage:`Defer the callback to be executed after the next DOM update
55+
cycle. Use it immediately after you’ve changed some data to wait
56+
for the DOM update. This is the same as the global ${colorArgs(`Vue.nextTick`)},
57+
except that the callback’s ${colorArgs(`this`)} context is automatically bound
58+
to the instance calling this method.
59+
${chalk.bold(`New in 2.1.0: returns a Promise if no callback is provided and
60+
Promise is supported in the execution environment`)}.
61+
62+
`,
63+
example: `
64+
${colorArgs(`new`)} Vue({
65+
${colorComment(`// ...`)}
66+
methods: {
67+
${colorComment(`// ...`)}
68+
example: ${chalk.blue(`function`)} () {
69+
${colorComment(`// modify data`)}
70+
${colorArgs(`this`)}.message = 'changed'
71+
${colorComment(`// DOM is not updated yet`)}
72+
${colorArgs(`this`)}.$nextTick(${chalk.blue(`function`)} () {
73+
${colorComment(`// DOM is now updated`)}
74+
${colorComment(`// 'this' is bound to the current instance`)}
75+
${colorArgs(`this`)}.doSomethingElse()
76+
})
77+
}
78+
}
79+
})
80+
81+
`
82+
}
83+
84+
const VM_$DESTROY = {
85+
category: cat,
86+
name: `vm.$destroy()`,
87+
usage: `Completely destroy a vm. Clean up its connections with other existing vms,
88+
unbind all its directives, turn off all event listeners.
89+
Triggers the ${colorArgs(`beforeDestroy`)} and ${colorArgs(`destroyed`)} hooks.
90+
91+
In normal use cases you shouldn’t have to call this method yourself.
92+
Prefer controlling the lifecycle of child components in a data-driven
93+
fashion using ${colorArgs(`v-if`)} and ${colorArgs(`v-for.`)}.`
94+
}
1495

1596
module.exports = {
1697
VM_$MOUNT,

logfunctions/loginstance.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ module.exports = {
77
`
88
${chalk.green('Category:')} ${obj.category}
99
${chalk.green('Name:')} ${obj.name + '\n'}
10-
* Arguments (${obj.arguments.length}):
11-
${obj.arguments.map((arg, index) => {
10+
${obj.arguments ? `* Arguments (${obj.arguments.length})` : ''}
11+
${obj.arguments ? obj.arguments.map((arg, index) => {
1212
return `${index+1}: ${args(arg)} `
13-
}).join(" ")}
13+
}).join(" ") : ''}
1414
${obj.returns ? `* Returns: ${args(obj.returns)}` : ''}
1515
* Usage: ${obj.usage}
1616
${obj.example ? `* Example: ${obj.example}` : ''}

vue-help.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const om = api.OptionsMisc
1515
const ip = api.InstanceProperties
1616
const id = api.InstanceData
1717
const im = api.InstanceEvents
18+
const il = api.InstanceLifecycle
1819

1920

2021
V.delimiter('vue-help$').show()
@@ -492,3 +493,27 @@ V.command('vm.$emit', 'Instance Methods/Events: #vm.$delete(event,[...args])').a
492493
})
493494

494495
// Instance Methods/Lifecycle
496+
497+
V.command('vm.$mount', 'Instance Methods/Lifecycle: #vm.$mount([elementOrSelector])').action(function(args, callback){
498+
const w = il[0]
499+
this.log(LI.logInstance(w))
500+
callback()
501+
})
502+
503+
V.command('vm.$forceUpdate', 'Instance Methods/Lifecycle: #vm.$forceUpdate()').action(function(args, callback){
504+
const w = il[1]
505+
this.log(LI.logInstance(w))
506+
callback()
507+
})
508+
509+
V.command('vm.$nextTick', 'Instance Methods/Lifecycle: #vm.$nextTick([callback])').action(function(args, callback){
510+
const w = il[2]
511+
this.log(LI.logInstance(w))
512+
callback()
513+
})
514+
515+
V.command('vm.$destroy', 'Instance Methods/Lifecycle: #vm.$destroy()').action(function(args, callback){
516+
const w = il[3]
517+
this.log(LI.logInstance(w))
518+
callback()
519+
})

0 commit comments

Comments
 (0)