angular-architects / ngrx-toolkit

Various Extensions for the NgRx Signal Store
MIT License
146 stars 22 forks source link

How do I call defined method from the same store? #46

Closed amrit-moomie closed 5 months ago

amrit-moomie commented 5 months ago

Is it possible to call a method in the store from another method in the same store?

Like example, I am not able to. I will get an error on Line 9 like Property 'markStateDirtyforId' does not exist on type.....


1. export const TabStore = signalStore(
2.     { providedIn: 'root' },
3.     withState<XYZ>(XYZSTATE),
4.     withMethods((store) => ({
5.         markStateDirtyforId: () => {
6.           return ........
7.         },
8.         openId: (id) => {
9.           store.markStateDirtyforId(id);
10.         }
11. 
12.     )
rainerhahnekamp commented 5 months ago

Yes, of course, just do:

export const TabStore = signalStore(
  { providedIn: 'root' },
  withState<XYZ>(XYZSTATE),
  withMethods((store) => {
    const markStateDirtyForId = () => {
      // implement your function here
    };

    return {
      markStateDirtyforId,
      openId: (id) => {
        markStateDirtyforId(id);
      }
    }
  })
)

An alternative would be:

export const TabStore = signalStore(
  { providedIn: 'root' },
  withState<XYZ>(XYZSTATE),  
  withMethods((store) => {
    return {
      markStateDirtyforId() {
      // your implementation here
      }
    };
  }),
  withMethods((store) => {
    return {
      openId: (id) => {
        store.markStateDirtyforId(id);
      }
    }
  })
)
amrit-moomie commented 5 months ago

Amazing