Skip to content

Commit 1e74675

Browse files
committed
expose Vue.set/delete on instances as vm.$set/$delete
1 parent 8e1478e commit 1e74675

File tree

4 files changed

+96
-70
lines changed

4 files changed

+96
-70
lines changed

flow/component.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ declare interface Component {
3232
$mount: (el?: Element | string, hydrating?: boolean) => Component;
3333
$forceUpdate: () => void;
3434
$destroy: () => void;
35+
$set: (obj: Array<any> | Object, key: any, val: any) => void;
36+
$delete: (obj: Object, key: string) => void;
3537
$watch: (expOrFn: string | Function, cb: Function, options?: Object) => Function;
3638
$on: (event: string, fn: Function) => Component;
3739
$once: (event: string, fn: Function) => Component;

src/core/instance/state.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import Watcher from '../observer/watcher'
44
import Dep from '../observer/dep'
55

66
import {
7+
set,
8+
del,
79
observe,
810
defineReactive,
911
observerState
@@ -192,6 +194,9 @@ export function stateMixin (Vue: Class<Component>) {
192194
}
193195
Object.defineProperty(Vue.prototype, '$data', dataDef)
194196

197+
Vue.prototype.$set = set
198+
Vue.prototype.$delete = del
199+
195200
Vue.prototype.$watch = function (
196201
expOrFn: string | Function,
197202
cb: Function,
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import Vue from 'vue'
2+
3+
describe('Instance methods data', () => {
4+
it('$set/$delete', done => {
5+
const vm = new Vue({
6+
template: '<div>{{ a.msg }}</div>',
7+
data: {
8+
a: {}
9+
}
10+
}).$mount()
11+
expect(vm.$el.innerHTML).toBe('')
12+
vm.$set(vm.a, 'msg', 'hello')
13+
waitForUpdate(() => {
14+
expect(vm.$el.innerHTML).toBe('hello')
15+
vm.$delete(vm.a, 'msg')
16+
}).then(() => {
17+
expect(vm.$el.innerHTML).toBe('')
18+
}).then(done)
19+
})
20+
21+
describe('$watch', () => {
22+
let vm, spy
23+
beforeEach(() => {
24+
vm = new Vue({
25+
data: {
26+
a: {
27+
b: 1
28+
}
29+
}
30+
})
31+
spy = jasmine.createSpy('watch')
32+
})
33+
34+
it('basic usage', done => {
35+
vm.$watch('a.b', spy)
36+
vm.a.b = 2
37+
waitForUpdate(() => {
38+
expect(spy.calls.count()).toBe(1)
39+
expect(spy).toHaveBeenCalledWith(2, 1)
40+
vm.a = { b: 3 }
41+
}).then(() => {
42+
expect(spy.calls.count()).toBe(2)
43+
expect(spy).toHaveBeenCalledWith(3, 2)
44+
}).then(done)
45+
})
46+
47+
it('immediate', () => {
48+
vm.$watch('a.b', spy, { immediate: true })
49+
expect(spy.calls.count()).toBe(1)
50+
expect(spy).toHaveBeenCalledWith(1)
51+
})
52+
53+
it('unwatch', done => {
54+
const unwatch = vm.$watch('a.b', spy)
55+
unwatch()
56+
vm.a.b = 2
57+
waitForUpdate(() => {
58+
expect(spy.calls.count()).toBe(0)
59+
}).then(done)
60+
})
61+
62+
it('function watch', done => {
63+
vm.$watch(function () {
64+
return this.a.b
65+
}, spy)
66+
vm.a.b = 2
67+
waitForUpdate(() => {
68+
expect(spy).toHaveBeenCalledWith(2, 1)
69+
}).then(done)
70+
})
71+
72+
it('deep watch', done => {
73+
var oldA = vm.a
74+
vm.$watch('a', spy, { deep: true })
75+
vm.a.b = 2
76+
waitForUpdate(() => {
77+
expect(spy).toHaveBeenCalledWith(oldA, oldA)
78+
vm.a = { b: 3 }
79+
}).then(() => {
80+
expect(spy).toHaveBeenCalledWith(vm.a, oldA)
81+
}).then(done)
82+
})
83+
84+
it('warn expresssion', () => {
85+
vm.$watch('a + b', spy)
86+
expect('Watcher only accepts simple dot-delimited paths').toHaveBeenWarned()
87+
})
88+
})
89+
})

test/unit/features/instance/methods-watch.spec.js

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)