Skip to content

Commit 6afe4c5

Browse files
committed
props default values
1 parent 2301f72 commit 6afe4c5

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

src/compiler/compile.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -425,16 +425,16 @@ var identRE = require('../parsers/path').identRE
425425
function compileProps (el, propDescriptors) {
426426
var props = []
427427
var i = propDescriptors.length
428-
var descriptor, name, assertions, value, path, prop, literal, single
428+
var descriptor, name, options, value, path, prop, literal, single
429429
while (i--) {
430430
descriptor = propDescriptors[i]
431431
// normalize prop string/descriptor
432432
if (typeof descriptor === 'object') {
433433
name = descriptor.name
434-
assertions = descriptor
434+
options = descriptor
435435
} else {
436436
name = descriptor
437-
assertions = null
437+
options = null
438438
}
439439
// props could contain dashes, which will be
440440
// interpreted as minus calculations by the parser
@@ -461,7 +461,7 @@ function compileProps (el, propDescriptors) {
461461
name: name,
462462
raw: value,
463463
path: path,
464-
assertions: assertions,
464+
options: options,
465465
mode: propBindingModes.ONE_WAY
466466
}
467467
if (value !== null) {
@@ -495,7 +495,7 @@ function compileProps (el, propDescriptors) {
495495
}
496496
}
497497
}
498-
} else if (assertions && assertions.required) {
498+
} else if (options && options.required) {
499499
_.warn('Missing required prop: ' + name)
500500
}
501501
props.push(prop)
@@ -519,7 +519,10 @@ function makePropsLinkFn (props) {
519519
path = prop.path
520520
if (prop.raw === null) {
521521
// initialize undefined prop
522-
vm._data[path] = undefined
522+
vm._data[path] = (
523+
prop.options &&
524+
prop.options.default
525+
) || undefined
523526
} else if (prop.dynamic) {
524527
// dynamic prop
525528
if (vm._context) {

src/util/misc.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ var config = require('../config')
99
*/
1010

1111
exports.assertProp = function (prop, value) {
12-
var assertions = prop.assertions
13-
if (!assertions) {
12+
var options = prop.options
13+
if (!options) {
1414
return true
1515
}
16-
var type = assertions.type
16+
var type = options.type
1717
var valid = true
1818
var expectedType
1919
if (type) {
@@ -48,7 +48,7 @@ exports.assertProp = function (prop, value) {
4848
)
4949
return false
5050
}
51-
var validator = assertions.validator
51+
var validator = options.validator
5252
if (validator) {
5353
if (!validator.call(null, value)) {
5454
_.warn(

test/unit/specs/compiler/compile_spec.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,11 @@ if (_.inBrowser) {
156156
'twoway',
157157
'with-filter',
158158
'camelCase',
159-
'boolean-literal'
159+
'boolean-literal',
160+
{
161+
name: 'default-value',
162+
default: 123
163+
}
160164
]
161165
var def = Vue.options.directives._prop
162166
el.setAttribute('a', '1')
@@ -204,7 +208,7 @@ if (_.inBrowser) {
204208
expect(args[3]).toBe(def)
205209
// literal and one time should've been set on the _data
206210
// and numbers should be casted
207-
expect(Object.keys(vm._data).length).toBe(5)
211+
expect(Object.keys(vm._data).length).toBe(6)
208212
expect(vm.a).toBe(1)
209213
expect(vm._data.a).toBe(1)
210214
expect(vm.someOtherAttr).toBe(2)
@@ -214,6 +218,7 @@ if (_.inBrowser) {
214218
expect(vm.booleanLiteral).toBe('from parent: true')
215219
expect(vm._data.booleanLiteral).toBe('from parent: true')
216220
expect(vm._data.camelCase).toBeUndefined()
221+
expect(vm._data.defaultValue).toBe(123)
217222
// camelCase should've warn
218223
expect(hasWarned(_, 'using camelCase')).toBe(true)
219224
})

0 commit comments

Comments
 (0)