Skip to content

Commit c24a4e8

Browse files
author
Guillaume Chau
authored
Improve [email protected] compat (vuejs#534)
* Test app * Fix data editing for Vue 1.x * Vuex test case * Fix for [email protected] getters
1 parent d25dd10 commit c24a4e8

File tree

3 files changed

+85
-3
lines changed

3 files changed

+85
-3
lines changed

src/backend/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -719,9 +719,13 @@ function setStateValue ({ id, path, value, newKey, remove }) {
719719
if (value) {
720720
parsedValue = parse(value, true)
721721
}
722+
const api = isLegacy ? {
723+
$set: hook.Vue.set,
724+
$delete: hook.Vue.delete
725+
} : instance
722726
set(instance._data, path, parsedValue, (obj, field, value) => {
723-
(remove || newKey) && instance.$delete(obj, field)
724-
!remove && instance.$set(obj, newKey || field, value)
727+
(remove || newKey) && api.$delete(obj, field)
728+
!remove && api.$set(obj, newKey || field, value)
725729
})
726730
} catch (e) {
727731
console.error(e)

src/backend/vuex.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function initVuexBackend (hook, bridge) {
66

77
const getSnapshot = () => stringify({
88
state: store.state,
9-
getters: store.getters
9+
getters: store.getters || {}
1010
})
1111

1212
bridge.send('vuex:init', getSnapshot())

vue1-test.html

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<html>
2+
<body>
3+
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
4+
<script src="https://unpkg.com/[email protected]/dist/vuex.js"></script>
5+
6+
<div id="app"></div>
7+
8+
<script>
9+
const store = new Vuex.Store({
10+
state: {
11+
counter: 0
12+
},
13+
mutations: {
14+
INCREMENT: state => state.counter++,
15+
DECREMENT: state => state.counter--,
16+
SET_COUNTER: (state, value) => state.counter = value
17+
}
18+
})
19+
20+
Vue.component('data-test', {
21+
template: `<div>
22+
{{ bool }} {{ text }} {{ number }}
23+
</div>`,
24+
data () {
25+
return {
26+
bool: false,
27+
text: 'hello world',
28+
number: 0
29+
}
30+
}
31+
})
32+
33+
Vue.component('vuex-test', {
34+
template: `<div>
35+
<div>{{ counter }} {{ isMoreThanTwo }}</div>
36+
<div>
37+
<button @click="$store.dispatch('INCREMENT')">+1</button>
38+
<button @click="$store.dispatch('DECREMENT')">-1</button>
39+
<button @click="$store.dispatch('SET_COUNTER', 0)">Reset</button>
40+
</div>
41+
</div>`,
42+
computed: {
43+
counter () {
44+
return this.$store.state.counter
45+
}
46+
},
47+
vuex: {
48+
getters: {
49+
isMoreThanTwo: state => state.counter > 2
50+
}
51+
}
52+
})
53+
54+
Vue.component('event-test', {
55+
template: `<div>
56+
<button @click="$emit('foo', 'bar')">Emit event</button>
57+
<button @click="$dispatch('foo', 'meow')">Dispatch event</button>
58+
<button @click="$broadcast('foo', 'waf')">Broadcast event</button>
59+
</div>`
60+
})
61+
62+
new Vue({
63+
el: '#app',
64+
store,
65+
template: `<div id="app">
66+
<data-test></data-test>
67+
<vuex-test></vuex-test>
68+
<event-test @foo="onFoo"></event-test>
69+
</div>`,
70+
methods: {
71+
onFoo (value) {
72+
console.log(value)
73+
}
74+
}
75+
})
76+
</script>
77+
</body>
78+
</html>

0 commit comments

Comments
 (0)