Skip to content

Commit 2814b94

Browse files
author
User
committed
updated Options/Data
1 parent aeae889 commit 2814b94

File tree

4 files changed

+79
-3
lines changed

4 files changed

+79
-3
lines changed

API.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ const OptionsData = [
4040

4141
module.exports = {
4242
GlobalConfig,
43-
GlobalAPI
43+
GlobalAPI,
44+
OptionsData
4445
}

apis/optionsdata.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,44 @@ const colorArgs = custom.colorArgs
88

99

1010
const DATA = {
11+
category: 'Options/Data',
12+
name: 'data',
13+
type: 'Object | Function',
14+
restrictions: `Only accepts ${colorArgs('Function')} when used in a component definition.`,
15+
details: `The data object for the Vue instance. Vue will recursively convert its properties into getter/setters
16+
to make it “reactive”. ${chalk.bold('The object must be plain')}: native objects such as browser API objects
17+
and prototype properties are ignored. A rule of thumb is that data should just be data -
18+
it is not recommended to observe objects with its own stateful behavior.Once observed, you
19+
can no longer add reactive properties to the root data object. It is therefore recommended
20+
to declare all root-level reactive properties upfront, before creating the instance. After the
21+
instance is created, the original data object can be accessed as ${colorArgs('vm.$data')}. The Vue instance
22+
also proxies all the properties found on the data object, so ${colorArgs('vm.a')} will be equivalent to
23+
${colorArgs('vm.$data.a')}. Properties that start with ${colorArgs('_')} or ${colorArgs('$')} will ${chalk.bold('not')} be proxied on the Vue instance because they may conflict
24+
with Vue’s internal properties and API methods. You will have to access them as ${colorArgs('vm.$data._property')}.
25+
When defining a ${chalk.bold('component')}, ${colorArgs('data')} must be declared as a function that returns the initial data object,
26+
because there will be many instances created using the same definition.
27+
If we still use a plain object for ${colorArgs('data')}, that same object will be ${chalk.bold('shared by reference')}
28+
across all instances created! By providing a ${colorArgs('data')} function, every time a new instance is created, we can simply call it to return a
29+
fresh copy of the initial data. If required, a deep clone of the original object can be obtained by passing
30+
${colorArgs('vm.$data')} through ${colorArgs('JSON.parse(JSON.stringify(...))')}.`,
31+
example: `
32+
${colorArgs('var')} data = { a: ${colorPrimitive('1')} }
33+
${colorComment('// direct instance creation')}
34+
${colorArgs('var')} vm = ${colorArgs('new')} Vue({
35+
data: data
36+
})
37+
vm.a ${colorComment('// -> 1')}
38+
vm.$data === data ${colorComment('// -> true')}
39+
${colorComment('// must use function when in Vue.extend()')}
40+
${colorArgs('var')} Component = Vue.extend({
41+
data: ${chalk.blue('function')} () {
42+
${colorArgs('return')} { a: ${colorPrimitive('1')} }
43+
}
44+
})
45+
Note that you should not use an arrow function with the ${colorArgs('data')}
46+
property (e.g. ${colorArgs('data: () => { return { a: this.myProp }}')} ). The reason is
47+
arrow functions bind the parent context, so ${colorArgs('this')} will not be the Vue instance
48+
as you expect and ${colorArgs('this.myProp')} will be undefined.`
1149

1250
}
1351

logfunctions/logoptions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ const chalk = require('chalk')
22
const custom = require('../customcolors')
33
const args = custom.colorArgs
44
module.exports = {
5-
logOption: function(obj){
5+
logOptions: function(obj){
66
return(
77
`
88
${chalk.green('Category:')} ${obj.category}
99
${chalk.green('Name:')} ${obj.name + '\n'}
1010
* Type: ${args(obj.type)}
1111
${obj.restrictions ? `* Restrictions: ${obj.restrictions}` : ''}
12-
* Details ${obj.details}
12+
* Details: ${obj.details}
1313
${obj.example ? `* Example: ${obj.example}` : ''}
1414
`
1515
)

vue-help.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const V = require('vorpal')()
22
const LGC = require('./logfunctions/logglobalconfig')
33
const LGA = require('./logfunctions/logglobalapi')
4+
const O = require('./logfunctions/logoptions')
45
const api = require('./API')
56
const gc = api.GlobalConfig
67
const ga = api.GlobalAPI
8+
const od = api.OptionsData
79

810
V.delimiter('vue-help$').show()
911

@@ -133,3 +135,38 @@ V.command('version', 'Global API: #Vue.version').action(function(args, callback)
133135

134136
// Options/Data
135137

138+
V.command('data', 'Options/Data: #data').action(function(args, callback){
139+
const d = od[0]
140+
this.log(O.logOptions(d))
141+
callback()
142+
})
143+
144+
V.command('props', 'Options/Data: #props').action(function(args, callback){
145+
const p = od[1]
146+
this.log(O.logOptions(p))
147+
callback()
148+
})
149+
150+
V.command('propsData', 'Options/Data: #propsData').action(function(args, callback){
151+
const pd = od[2]
152+
this.log(O.logOptions(pd))
153+
callback()
154+
})
155+
156+
V.command('computed', 'Options/Data: #computed').action(function(args, callback){
157+
const c = od[3]
158+
this.log(O.logOptions(c))
159+
callback()
160+
})
161+
162+
V.command('methods', 'Options/Data: #methods').action(function(args, callback){
163+
const m = od[4]
164+
this.log(O.logOptions(m))
165+
callback()
166+
})
167+
168+
V.command('watch', 'Options/Data: #watch').action(function(args, callback){
169+
const w = od[5]
170+
this.log(O.logOptions(w))
171+
callback()
172+
})

0 commit comments

Comments
 (0)