From 5e262da851716bfcada9b7c5297e54c2dded33cf Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Fri, 17 Jan 2020 11:57:48 +0100 Subject: [PATCH] fix: bind the actions to the store Allow using @click="store.login" by always setting the context as the store --- __tests__/actions.spec.ts | 7 +++++++ src/store.ts | 6 +++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/__tests__/actions.spec.ts b/__tests__/actions.spec.ts index bd78a826..fb309e32 100644 --- a/__tests__/actions.spec.ts +++ b/__tests__/actions.spec.ts @@ -55,6 +55,13 @@ describe('Store', () => { expect(store.state.a).toBe(false) }) + it('store is forced as the context', () => { + const store = useStore() + expect(store.state.a).toBe(true) + store.toggle.call(null) + expect(store.state.a).toBe(false) + }) + it('can call other actions', () => { const store = useStore() expect(store.state.a).toBe(true) diff --git a/src/store.ts b/src/store.ts index 52cbca12..0168e5f1 100644 --- a/src/store.ts +++ b/src/store.ts @@ -184,7 +184,7 @@ export function buildStore< wrappedActions[actionName] = function() { setActiveReq(_r) // eslint-disable-next-line - return actions[actionName].apply(this, arguments as unknown as any[]) + return actions[actionName].apply(store, arguments as unknown as any[]) } as StoreWithActions[typeof actionName] } @@ -261,7 +261,7 @@ export function createStore< // allow actions use other actions actions?: A & ThisType & StoreWithGetters> }) { - const { id, state: buildState, getters, actions } = options + const { id, state, getters, actions } = options return function useStore(): Store { const req = getActiveReq() @@ -272,7 +272,7 @@ export function createStore< if (!store) { stores[id] = store = buildStore( id, - buildState, + state, getters, actions, getInitialState(id) -- 2.47.2