From: Eduardo San Martin Morote Date: Fri, 17 Jan 2020 10:57:48 +0000 (+0100) Subject: fix: bind the actions to the store X-Git-Tag: 0.0.5~15 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5e262da851716bfcada9b7c5297e54c2dded33cf;p=thirdparty%2Fvuejs%2Fpinia.git fix: bind the actions to the store Allow using @click="store.login" by always setting the context as the store --- 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)