Skip to content

Commit

Permalink
fix prop default value observing when data() is also present (fix vue…
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jul 23, 2015
1 parent eb95383 commit 7b6913e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/instance/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ exports._initData = function () {
if (optionsData) {
this._data = optionsData
for (var prop in propsData) {
if (this._props[prop].raw !== null) {
if (
this._props[prop].raw !== null ||
!optionsData.hasOwnProperty(prop)
) {
optionsData.$set(prop, propsData[prop])
}
}
Expand Down
31 changes: 31 additions & 0 deletions test/unit/specs/directives/prop_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,37 @@ if (_.inBrowser) {
})
expect(vm.$children[0].prop).toBe(true)
expect(vm.$el.textContent).toBe('true')
expect(JSON.stringify(vm.$children[0].$data)).toBe(JSON.stringify({
prop: true
}))
})

it('should initialize with default value when not provided & has default data', function () {
var vm = new Vue({
el: el,
template: '<test></test>',
components: {
test: {
props: {
prop: {
type: String,
default: 'hello'
}
},
data: function () {
return {
other: 'world'
}
},
template: '{{prop}} {{other}}'
}
}
})
expect(vm.$el.textContent).toBe('hello world')
expect(JSON.stringify(vm.$children[0].$data)).toBe(JSON.stringify({
other: 'world',
prop: 'hello'
}))
})
})
}

0 comments on commit 7b6913e

Please sign in to comment.