This is my first post . I would like to discuss whether it is appropiate to put business logic in Ionic pages. I’m new to Ionic and having been playing with it for a couple of weeks I have some doubts regarding app structure.
I’m currently trying to implement a small app that consumes a REST API. Having stopped to learn Angular first for a week before starting with Ionic, I learned that Angular Components should not have any heavy business logic inside them, as they are meant to be as simple as they can get, so modularity is not compromised. Services in Angular are the ones in which the application business logic relies.
I come from a Java background and I have some experience developing native Android, in which each page (activity or fragment) has it’s business logic implemented right there.
Now for me it’s a bit confusing, AFAIK Ionic pages are Angular Components in the end, but it would be familiar for me to put business logic into them, sadly I don’t really know if I should… Let me give you an example, I ran into this situation:
I have a login page, which should log the user in and store some data coming from the server with the response to the LOGIN request. Here you can think of two Angular services to use, one is the UserService, providing user related functionality and the other would be the StorasgeService, providing storage functionality.
Here would be the application flow:
- User enters credentials and clicks LOGIN
- App fires a POST request to /login with username and password
- Auth OK @ backend: Server responds with a token which the app receives
- App stores the token in the device
- App asks for user info using this token received with a GET req to the server
- App receives user info and checks if account is confirmed
- If account is confirmed, you can continue to the next page. If not, an error message shows up
Thinking of this application flow, I am tempted to implement all this business logic inside the LoginPage.ts*. This page relies on UserService and StorageService to make requests to the server and to store the token inside the device, respectively, but the business logic is implemented in one method inside the login page TS file.
I don’t know if you think this approach is acceptable or if it would be better to have a “doLogin()” method inside LoginPage.ts which only called a login() method in UserService and then implement all this business logic inside that Service.
I hope I could explain myself well and very much thanks for your time!