Skip to content

Commit 949ada9

Browse files
committed
copy props when replacing $data (fix vuejs/Discussion#173)
1 parent 5786cb1 commit 949ada9

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/instance/scope.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ exports._setData = function (newData) {
5959
var oldData = this._data
6060
this._data = newData
6161
var keys, key, i
62+
// copy props
63+
var props = this.$options.props
64+
if (props) {
65+
i = props.length
66+
while (i--) {
67+
key = props[i]
68+
newData.$set(key, oldData[key])
69+
}
70+
}
6271
// unproxy keys not present in new data
6372
keys = Object.keys(oldData)
6473
i = keys.length

test/unit/specs/instance/scope_spec.js

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

3232
})
3333

34+
describe('$data', function () {
35+
36+
var vm = new Vue({
37+
props: ['c'],
38+
data: {
39+
a: 1
40+
}
41+
})
42+
43+
it('should initialize props', function () {
44+
expect(vm.hasOwnProperty('c')).toBe(true)
45+
})
46+
47+
it('replace $data', function () {
48+
vm.c = 3
49+
vm.$data = { b: 2 }
50+
// proxy new key
51+
expect(vm.b).toBe(2)
52+
// unproxy old key that's no longer present
53+
expect(vm.hasOwnProperty('a')).toBe(false)
54+
// should copy props
55+
expect(vm.c).toBe(3)
56+
})
57+
58+
})
59+
3460
describe('computed', function () {
3561

3662
var Test = Vue.extend({

0 commit comments

Comments
 (0)