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.

Luillyfe
  • 4,659
  • 8
  • 28
  • 41

2 Answers2

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

Riaz as kather
  • 561
  • 6
  • 13
  • 1
    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: https://docs.strongloop.com/display/public/LB/Create+a+simple+API – Luillyfe Feb 24 '16 at 19:20
  • 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 models` – rahpuser Aug 28 '16 at 21:49
  • 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? – YeeHaw1234 Apr 12 '17 at 19:37
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)

YeeHaw1234
  • 1,720
  • 14
  • 15
  • What would be the advantage(s) of the model used in the browser being the same as the model used on the server? – VK1 May 17 '19 at 05:13