]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
fix: bind the actions to the store
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 17 Jan 2020 10:57:48 +0000 (11:57 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 20 Jan 2020 18:21:18 +0000 (19:21 +0100)
Allow using @click="store.login" by always setting the context as the
store

__tests__/actions.spec.ts
src/store.ts

index bd78a826f3443616e1ee10169dc1e95b5ff9baf1..fb309e322fb40e998b206875e7f2e674ffc9dae1 100644 (file)
@@ -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)
index 52cbca122fab64c910b4de60f676b316330575c9..0168e5f10f6ff1fd2123d1b5012e644786cbd282 100644 (file)
@@ -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<A>[typeof actionName]
   }
 
@@ -261,7 +261,7 @@ export function createStore<
   // allow actions use other actions
   actions?: A & ThisType<A & StoreWithState<Id, S> & StoreWithGetters<S, G>>
 }) {
-  const { id, state: buildState, getters, actions } = options
+  const { id, state, getters, actions } = options
 
   return function useStore(): Store<Id, S, G, A> {
     const req = getActiveReq()
@@ -272,7 +272,7 @@ export function createStore<
     if (!store) {
       stores[id] = store = buildStore(
         id,
-        buildState,
+        state,
         getters,
         actions,
         getInitialState(id)