Comments (8)
@deebloo - I have an idea for this, will be pretty easy to implement too.
@State({ ... })
export class ZooState {
@Selector({
combine: [CowState.cows, ...n*]
})
static animals(state: ZooStateModel, cows: Cows, ...n*) { ... }
}
from store.
I would prefer
@Selector([CowState.cows, FarmState.milktime])
from store.
Ya, I was trying to keep it where if we wanted to add other options down the road it would be easy but ya I agree
from store.
I have spent some time thinking about this feature.
I have tried to define clear rules on what would be expected from this.
Here are my thoughts on this:
- (This is the existing usage) A
@Selector
decorator with zero parameters is only valid in a state class and provides the container class' state model as the only parameter :
@State<UserStateModel>( ... )
export class UserState {
@Selector()
getUsers(userStateModel: UserStateModel) { ... }
}
- Allow state classes to be passed in as parameters. This can be used from within any class, be it a State class or another service class. It only provides the models as requested (ie. it does not add the model for the containing class, no matter if it is defined in a state class or not) :
export class SomeStateClassOrRegularService {
@Selector([UserState, TodoState])
getActiveUserTodos(userState: UserStateModel, todoState: TodoStateModel) { ... }
}
- Allow selectors to be passed in. The rules would be the same as passing State Classes as parameters:
export class SomeStateClassOrRegularService {
@Selector([UserState.getName, TodoState.getCount])
getUserTodoCount(userName: string, todoCount: number) { ... }
}
- Or a mix of the two:
export class SomeStateClassOrRegularService {
@Selector([UserState.getName, TodoState])
getUserTodos(userName: string, todoState: TodoStateModel) { ... }
}
- A
@Selector
with no parameters would not be valid on a class that is not a state. There was an option that the global state could then be passed as the parameter, but this would break encapsulation because it would require the dev to have knowledge of the global state structure.
To be clear, this would not be valid:
export class MyNonStateClass {
@Selector() // ERROR!
static invalidSelector(thisIsNotValid: any) { ... }
}
Regarding the possibility later of adding options, I think that the options could be expressed as an object and the decorator could detect if an options object or an array was passed to it or both. As an example the only thing I could think of now that may be an option we could add in future is a different memoiser, so this is what this could look like:
export class SomeStateClassOrRegularService {
@Selector([UserState.getName, TodoState], { memoizer: customMemoizer(3) })
getUserTodos(userName: string, todoState: TodoStateModel) { ... }
}
PS. selector option given as an example but I don't think we need to implement the options part until there is an actual need for it.
from store.
What would be the use case for a selector that doesn't belong to a state?
from store.
I think that the idea would be that if you need to create a selector that does a query across multiple states in your application and they do not share a common heirarchy (other than app state) then you could put this selector in a class not directly associated with a particular state class.
@leon mentioned this concept on slack in this comment: https://ngxs.slack.com/archives/CA0NS79MG/p1524210374000089
Maybe @deebloo can chip in here too.
from store.
Use Case for Seperate Selectors:
If I have a user module and multiple modules with relations to the user module and I want a selector pulling in a user with its related items. I would like to have this selector separate from any State. Because if I would put it in my UserState
it break lazy loading of user related feature modules.
@markwhitfeld thats some great work, I like those ideas. I think most important is passing in other selectors.
from store.
Closing and marking for future.
from store.
Related Issues (20)
- 📚[DOCS]: ZooState imported before it exists HOT 2
- 🐞[BUG]: ngxs-cli - generated code - contains nbsp and the spec uses deprecated code HOT 1
- 🐞[BUG]: Using Angular 16 webapp is throwing "inject() must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used with `runInInjectionContext` HOT 3
- best solution in impliment microfrontEnd HOT 1
- 🚀[FEATURE]: Support latest angular and typescript versions (decorators arent working) HOT 5
- 🐞[BUG]: Uncaught TypeError: Cannot read properties of undefined (reading 'SystemState') HOT 7
- 🐞[BUG]: @Select decorator doesn't trigger value HOT 1
- 🐞[BUG]: it seems that Ngxs is not compatible with Jest ESM HOT 4
- 🐞[BUG]: patch operator does not handle null existing values
- 🐞[BUG]: export 'StateContext' (imported as 'StateContext') was not found in '@ngxs/store' HOT 2
- 🚀[FEATURE]: How to Load New State at Runtime? HOT 1
- 🐞[BUG]: Could not use NgxsFormPlugin in subModule on Federation Architecture HOT 1
- 🚀[FEATURE]: Support Angular 17 HOT 9
- 🐞[BUG]: NgxsFormPlugin not work in Angular17 HOT 1
- 🐞[BUG]: ngxs-store.js: Invalid count value HOT 16
- 🐞[BUG]: ResetForm action side effect completes after the action
- 🚀[FEATURE]: Allow Actions with non-static type fields HOT 2
- NoInfer TS 5.4 HOT 2
- 🐞[BUG]: Cannot read properties of undefined (reading 'NGXS_META') HOT 2
- 📚[DOCS]: Setup on application root level HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from store.