-1

In the platform I am building there are users that you can follow or unfollow. Also, if you are looking at your own profile, you can edit it (instead of following it).

Currently, I have one state /profile/:id that renders the user profile with angularjs

I have one template _profile.html and a controller profileCrtl. The controller is responsible to check if the profile is authenticated and therefore to set different variables to show different HTML tags. For example, if the user is seeing his own profile, we are not going to show the following button. Instead, he/she will see an edit button to allow the user to edit his/her profile.

The problem with this approach is that porfileCtrl and _profile.html are responsible for manage the public profile user view and the private profile view and I think it is not a good design pattern.

I am considering three solutions to improve my code:

  1. To use two templates, _publicProfile, and _privateProfile and keep only one controller profileCtrl. We will have two states.

  2. To use two templates, _publicProfile and _privateProfile and to have two controllers. profilePublicCtrl and profilePrivateCtrl. (I feel this may be the best solution). We will have two states.

  3. To have only one state and change templateUrl dynamically if the user is the authenticated user or a public user.

What do you think is the best solution? Do you have any alternative to the ones considered above? When and wherein the code should I check if the profile the user is viewing is a public profile or the user’s profile?

I have already looked up a lot of answers online and there seems to be a lot of opinions and I find difficult to know what is the best design pattern.

gorkem
  • 731
  • 1
  • 10
  • 17
Ekaitz Hernandez Troyas
  • 1,147
  • 2
  • 11
  • 18

1 Answers1

1

I am facing the same problem with my App and I thing the best approach would be to use different template and separate controller for both the views. Explaining all the scenarios here -

1) You can use first approach to use to different templates with same controller but I will not prefer that, always try to provide dedicated controller to your templates.
2) Using to different controller with dedicated views is the best approach here. In this case you will get the full control over what data to show on public profile and what to show on private profile.
-- But the issues in this approach is, you will end up with lot of duplicate code on both the template
3) This is also good if you can manage it well within your profile template. You need to manage conditions based on user inputs every time.

Or you can create a separate directive which will take some parameters and manage all public/private condition in that directive. Within a single code you will get a reusable user profile and you can use that directive anywhere you want to show user profile.

Uday Ghulaxe
  • 45
  • 1
  • 6