Loopback: Property setters and getters

Created on 4 May 2016  路  5Comments  路  Source: strongloop/loopback

Hi guys, I would like to know how safe is the usage of setter and getter properties from model class.

This is already being used in loopback user model and can be seen here: User Code

Why is this not documented anywhere? Can we use it?

doc stale team-apex

Most helpful comment

@crandmck, I would talk to @raymondfeng about this. I haven't used _setters_ or _getters_ within LoopBack nor have I seen them used on a user-defined model, so I don't have an example but they are generally used to _get_ and _set_ private variables.

The code that defines them is in the model-builder in loopback-datasource-juggler if that helps at all.

@raymondfeng, would also be a good person to consult with on how much you should document and where that documentation should be located. I don't believe that using _getters/setters_ is a typical use case for models for the typical LoopBack user and could confuse people starting out with LoopBack.

All 5 comments

@richardpringle Can you help to clarify this so I can document? Here are some questions I have:

  • Why would one want to do this (examples)?
  • Can you do it for any model or just for built-in models? The example referred to above is for a built-in model.
  • In <Model>.setter['myProp'] = function(val) {}; is val just the value of the property being set (that you can then manipulate)? That's what it looks like from the example in user.js but I want to confirm.
  • As asked above, is this safe to do?

@crandmck, I would talk to @raymondfeng about this. I haven't used _setters_ or _getters_ within LoopBack nor have I seen them used on a user-defined model, so I don't have an example but they are generally used to _get_ and _set_ private variables.

The code that defines them is in the model-builder in loopback-datasource-juggler if that helps at all.

@raymondfeng, would also be a good person to consult with on how much you should document and where that documentation should be located. I don't believe that using _getters/setters_ is a typical use case for models for the typical LoopBack user and could confuse people starting out with LoopBack.

@raymondfeng Please advise ^.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings