What is the difference between common and server models in loopback?
Asked Answered
U

2

20

When I'm creating a model in strongloop with the following command:

slc loopback:model

loopback asks me for choose between common model or server.

Common model or server only?

I really don't know what criteria to take into account to choose between one option or the other. I'll be thankful if any of you can help me to understand or give me any insights to take a wise decision.

Untrimmed answered 23/2, 2016 at 22:36 Comment(0)
H
25

If you want to be able to share models between client and server parts of your app, put your model JSON and JavaScript files in the /common/models directory. If you want to have separate client and server models, then put your model JSON and JavaScript files in the /server/models directory.

Refer this link. https://docs.strongloop.com/display/public/LB/common+directory

Hinkle answered 24/2, 2016 at 7:21 Comment(3)
Just one more thing to add. In the below page you can add a little bit of information about the questions that CLI loopback will asks you: docs.strongloop.com/display/public/LB/Create+a+simple+APIUntrimmed
Why loopback suggest to keep common/models always ? Keep, the default, common, even though in this application you'll only be working with server-side modelsComstock
This answer is what is written in the documentation, but what exactly does it mean to "share" between client and server. If I put a model in the /server/models folder, what happens on the client and on the server?Mainsail
M
6

Loopback can be run on both the client and the server using isomorphic LoopBack. The same app running in NodeJs can also run in the client browser so instead of coding HTTP requests and responses on the client to CRUD your LoopBack model instances, you can simply call the appropriate loopback method.

In other words, instead of calling a POST to an API end-point to create a new object (POST /api/MyObject) in your database, you can simply call MyObject.create(data) on the client and the LoopBack app in the client will make the call for you. And instead of returning an HTTP response, it will return the created object.

Now, this brings us to the difference between /common and /server models: if you want the model used in the browser to be the same as the model used on the server, create your model in /common. If you need the model to be different, create the model in the /server/models folder (not sure where to create the model for the client)

Mainsail answered 5/12, 2017 at 20:2 Comment(1)
What would be the advantage(s) of the model used in the browser being the same as the model used on the server?Centner

© 2022 - 2024 — McMap. All rights reserved.