Skip to content

Commit

Permalink
expose replaceState()
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jul 1, 2016
1 parent f97d106 commit 49c17e1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
4 changes: 4 additions & 0 deletions docs/en/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ const store = new Vuex.Store({ ...options })
})
```
- **replaceState(state: Object)**
Replace the store's root state. Use this only for state restoration / time-travel purposes.

- **watch(getter: Function, cb: Function, [options: Object])**

Reactively watch a getter function's return value, and call the callback when the value changes. The getter receives the store's state as the only argument. Accepts an optional options object that takes the same options as Vue's `vm.$watch` method.
Expand Down
14 changes: 13 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,19 @@ class Store {
}

set state (v) {
throw new Error('[vuex] Vuex root state is read only.')
throw new Error('[vuex] Use store.replaceState() to explicit replace store state.')
}

/**
* Replace root state.
*
* @param {Object} state
*/

replaceState (state) {
this._dispatching = true
this._vm.state = state
this._dispatching = false
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/plugins/devtool.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ export default function devtoolPlugin (store) {
hook.emit('vuex:init', store)

hook.on('vuex:travel-to-state', targetState => {
store._dispatching = true
store._vm.state = targetState
store._dispatching = false
store.replaceState(targetState)
})

store.on('mutation', (mutation, state) => {
Expand Down

0 comments on commit 49c17e1

Please sign in to comment.