Mean.io - Best practice to extend User model

2019-06-01 10:16发布

问题:

Mean.io comes with a built in user model within the user package. What is the best practice for extending that user model if I want to attach additional data to it?

My experience with Django had me creating a "profile" that had a foreign key pointing towards the user object it belonged to. I like this approach because I don't touch the user package that way. But is this a best practice? If this is, how can I ensure the creation of a profile doc at the creation of a user doc? If not, what is?

回答1:

I'm not sure qm69's solution would be the best for future compatibility with mean. In the mean.io documentation http://learn.mean.io/ it states the developer shouldn't alter any core packages, including the user package.

The mean.io pattern is to implement any and all extensions as a custom package. And override default views using the $viewPathProvider.override method.

Secondly the User package is fundamentally a security/authentication feature and not a profile implementation which regularly receives updates. Altering this will most likely break future fixes and risk introducing security bugs.

My advice would be to implement a profile using means package system and add a service dependency for the User service. I've done this in previous projects and it works well.

To implement a profile package, follow the below steps:

1) Create a custom package called profile using mean package profile.

2) Implement model/view/control for all profile requirements in the custom package. DONT ALTER ANYTHING IN THE USER package.

2) Use dependency injection to include the Global service service. This will give you access to Global.user data so you most likely don't even need to use the User services.

3) Override any User views using the $override method mentioned in the above doco.

Hope this helps ;)