Skip to content

Commit 8f6dfce

Browse files
committed
initialize props properly
1 parent 966c3c9 commit 8f6dfce

File tree

4 files changed

+56
-12
lines changed

4 files changed

+56
-12
lines changed

src/compiler/compile.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -456,18 +456,18 @@ function compileProps (el, propDescriptors) {
456456
)
457457
}
458458
value = el.getAttribute(name)
459+
// create a prop descriptor
460+
prop = {
461+
name: name,
462+
raw: value,
463+
path: path,
464+
assertions: assertions,
465+
mode: propBindingModes.ONE_WAY
466+
}
459467
if (value !== null) {
460468
// important so that this doesn't get compiled
461469
// again as a normal attribute binding
462470
el.removeAttribute(name)
463-
// create a prop descriptor
464-
prop = {
465-
name: name,
466-
raw: value,
467-
path: path,
468-
assertions: assertions,
469-
mode: propBindingModes.ONE_WAY
470-
}
471471
var tokens = textParser.parse(value)
472472
if (tokens) {
473473
if (el && el.nodeType === 1) {
@@ -495,10 +495,10 @@ function compileProps (el, propDescriptors) {
495495
}
496496
}
497497
}
498-
props.push(prop)
499498
} else if (assertions && assertions.required) {
500499
_.warn('Missing required prop: ' + name)
501500
}
501+
props.push(prop)
502502
}
503503
return makePropsLinkFn(props)
504504
}
@@ -517,7 +517,10 @@ function makePropsLinkFn (props) {
517517
while (i--) {
518518
prop = props[i]
519519
path = prop.path
520-
if (prop.dynamic) {
520+
if (prop.raw === null) {
521+
// initialize undefined prop
522+
vm._data[path] = undefined
523+
} else if (prop.dynamic) {
521524
// dynamic prop
522525
if (vm.$parent) {
523526
if (prop.mode === propBindingModes.ONE_TIME) {

src/instance/scope.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,15 @@ exports._initData = function () {
4949
var optionsDataFn = this.$options.data
5050
var optionsData = optionsDataFn && optionsDataFn()
5151
if (optionsData) {
52-
this._data = _.extend(optionsData, propsData)
52+
this._data = optionsData
53+
for (var prop in propsData) {
54+
if (
55+
!optionsData.hasOwnProperty(prop) ||
56+
propsData[prop] !== undefined
57+
) {
58+
optionsData[prop] = propsData[prop]
59+
}
60+
}
5361
}
5462
var data = this._data
5563
// proxy data on instance

test/unit/specs/compiler/compile_spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,12 @@ if (_.inBrowser) {
204204
expect(args[3]).toBe(def)
205205
// literal and one time should've been set on the _data
206206
// and numbers should be casted
207-
expect(Object.keys(vm._data).length).toBe(4)
207+
expect(Object.keys(vm._data).length).toBe(5)
208208
expect(vm._data.a).toBe(1)
209209
expect(vm._data.someOtherAttr).toBe(2)
210210
expect(vm._data.onetime).toBe('from parent: a')
211211
expect(vm._data.booleanLiteral).toBe('from parent: true')
212+
expect(vm._data.camelCase).toBeUndefined()
212213
// camelCase should've warn
213214
expect(hasWarned(_, 'using camelCase')).toBe(true)
214215
})

test/unit/specs/instance/scope_spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,38 @@ describe('Instance Scope', function () {
3333

3434
describe('$data', function () {
3535

36+
it('should initialize props', function () {
37+
var vm = new Vue({
38+
el: document.createElement('div'),
39+
props: ['c']
40+
})
41+
expect(vm.hasOwnProperty('c')).toBe(true)
42+
})
43+
44+
it('should use default prop value if prop not provided', function () {
45+
var vm = new Vue({
46+
el: document.createElement('div'),
47+
props: ['c'],
48+
data: {
49+
c: 1
50+
}
51+
})
52+
expect(vm.c).toBe(1)
53+
})
54+
55+
it('prop should overwrite default value', function () {
56+
var el = document.createElement('div')
57+
el.setAttribute('c', '2')
58+
var vm = new Vue({
59+
el: el,
60+
props: ['c'],
61+
data: {
62+
c: 1
63+
}
64+
})
65+
expect(vm.c).toBe(2)
66+
})
67+
3668
it('replace $data', function () {
3769
var vm = new Vue({
3870
data: {

0 commit comments

Comments
 (0)