"...place code that interacts with the backend in separate files, and not directly in the view models"


#1

Tutorial [https://docs.nativescript.org/tutorial/chapter-4] says the following:

NOTE: In a larger app, it’s pretty common to place code that interacts with the backend in separate files, and not directly in the view models. But in our case, the connection code lives directly in the view model for simplicity—which is perfectly reasonable for small apps.

Could somebody show example what does it means? How to organize code more correctly for large app? What part of code should be contained in view model and what in separate file, and how they should be interact.


#2

Well it means put code in separate files and\or folders and expose the objects or methods needed.

Like in the hello world app…

The code for the viewmodel exists in a seperate file, so it’s just imported

…then used

All the complexity is abstracted over to a different file, but that file can exist anywhere

Like this app imports the code from a subfolder


#3

Your answer does not contain example of separating interacting with backend server from the view-model.
My question is about why in large apps view-model should not contain code that interacts with backend server? And how it should be organized (best practice).


#4

Right it technically doesn’t :slight_smile: But I was just trying to show you in a sample HOW you would go about that. Make a new file, put the code in there, import it from where you need it.

Don’t put too much stock in “should not contain code”, it perfectly well could. It’s just in an enterprise\large app you want abstraction to make testing easier, or more readable. This isn’t a nativescript concept, just general nicer way to handle things.

For ME (In a plain TS app) I have 3 root folders, /views, /scripts (or helpers), and /models. So views are my xml\ts view code, helpers or custom classes are in /scripts and /models are my… models.