Skip to content

Improve [email protected] compat #534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -719,9 +719,13 @@ function setStateValue ({ id, path, value, newKey, remove }) {
if (value) {
parsedValue = parse(value, true)
}
const api = isLegacy ? {
$set: hook.Vue.set,
$delete: hook.Vue.delete
} : instance
set(instance._data, path, parsedValue, (obj, field, value) => {
(remove || newKey) && instance.$delete(obj, field)
!remove && instance.$set(obj, newKey || field, value)
(remove || newKey) && api.$delete(obj, field)
!remove && api.$set(obj, newKey || field, value)
})
} catch (e) {
console.error(e)
Expand Down
2 changes: 1 addition & 1 deletion src/backend/vuex.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function initVuexBackend (hook, bridge) {

const getSnapshot = () => stringify({
state: store.state,
getters: store.getters
getters: store.getters || {}
})

bridge.send('vuex:init', getSnapshot())
Expand Down
78 changes: 78 additions & 0 deletions vue1-test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<html>
<body>
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
<script src="https://unpkg.com/[email protected]/dist/vuex.js"></script>

<div id="app"></div>

<script>
const store = new Vuex.Store({
state: {
counter: 0
},
mutations: {
INCREMENT: state => state.counter++,
DECREMENT: state => state.counter--,
SET_COUNTER: (state, value) => state.counter = value
}
})

Vue.component('data-test', {
template: `<div>
{{ bool }} {{ text }} {{ number }}
</div>`,
data () {
return {
bool: false,
text: 'hello world',
number: 0
}
}
})

Vue.component('vuex-test', {
template: `<div>
<div>{{ counter }} {{ isMoreThanTwo }}</div>
<div>
<button @click="$store.dispatch('INCREMENT')">+1</button>
<button @click="$store.dispatch('DECREMENT')">-1</button>
<button @click="$store.dispatch('SET_COUNTER', 0)">Reset</button>
</div>
</div>`,
computed: {
counter () {
return this.$store.state.counter
}
},
vuex: {
getters: {
isMoreThanTwo: state => state.counter > 2
}
}
})

Vue.component('event-test', {
template: `<div>
<button @click="$emit('foo', 'bar')">Emit event</button>
<button @click="$dispatch('foo', 'meow')">Dispatch event</button>
<button @click="$broadcast('foo', 'waf')">Broadcast event</button>
</div>`
})

new Vue({
el: '#app',
store,
template: `<div id="app">
<data-test></data-test>
<vuex-test></vuex-test>
<event-test @foo="onFoo"></event-test>
</div>`,
methods: {
onFoo (value) {
console.log(value)
}
}
})
</script>
</body>
</html>