Create model object base on properties ?

11 views
Skip to first unread message

Rodolfo hernandez

unread,
Oct 20, 2016, 11:55:03 AM10/20/16
to LoopbackJS
Having the model Instance is possible to create an instance of that model go get a loopback object model ?

For example:

var rawObject = {
 id: 1,
 name: 'ObjectName'
};

var Model = loopback.getModel('Model');
var modelInstance = new Model(rawObject);

And now I will have all model instances methods as toJSON method to that would filter what properties are hide or not.

Is this possible ?

Heath Morrison

unread,
Oct 21, 2016, 4:08:56 AM10/21/16
to LoopbackJS
Hello Rodolfo,

Yes, it's actually exactly as you've written - use the constructor and it will return a fully fledged (non-persisted) model instance. My only suggestion is that you access models using app.models instead of the global loopback.getModel. 

--
You received this message because you are subscribed to the Google Groups "LoopbackJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to loopbackjs+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/loopbackjs/70b97ff7-4927-4a64-a253-74517f0b0045%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages